如何独特的是兰特(c)中? [英] How unique is rand() in C?

查看:147
本文介绍了如何独特的是兰特(c)中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用兰特()于需要唯一值的6位字段。我这样做对吗?

I am using rand() for a 6 digit field which needs unique values. Am I doing it right?

有什么胜算,兰特()能给我相似的价值观连续或频繁调用?

What are the odds, rand() can give me similar values on consecutive or frequent calls?

当我用兰特这是唯一的()。但是,返回相同数量的时候我叫函数srand(时间(NULL))函数srand(时钟())。看来,喜欢它的工作相对来说我。或者是什么?

It was unique when I used rand(). But, returned same number when I called srand(time(NULL)) or srand(clock()). Seems, like it's working opposite for me. Or is it?

推荐答案

正如其他人所指出的,唯一性无法得到保证。然而,你是因为你使用srand()函数和RAND()不正确可能看到重复的数字。

As others have pointed out, uniqueness is not guaranteed. However you are probably seeing repeated numbers because you are using srand() and rand() incorrectly.

srand()函数是用来播种随机数发生器。这意味着一系列调用调用函数srand会产生一系列特定的值后RAND()的。如果调用srand()函数具有相同的值,然后兰特()会产生同一系列的值(对于给定的实施,有不同的实现之间没有担保)

srand() is used to seed the random number generator. that means a series of calls to rand() after a call to srand will produce a particular series of values. If you call srand() with the same value then rand() will produce the same series of values (for a given implementation, there's no guarantee between different implementations)

int main() {
    srand(100);
    for(int i = 0; i<5; ++i)
        printf("%d\n",rand());

    printf("\nreset\n\n");

    srand(100);
    for(int i = 0; i<5; ++i)
        printf("%d\n",rand());

}

这对我来说会产生:

for me this produces:

365
1216
5415
16704
24504

reset

365
1216
5415
16704
24504

时间()和时钟()返回的时间,但如果你给他们打电话的速度不够快,则返回的值是一样的,所以你会得到相同的序列值出兰特()。

time() and clock() return the time, but if you call them quickly enough then the value returned will be the same, so you will get the same series of values out of rand().

此外兰特()一般不会有很好的随机数生成器并使用它通常意味着你必须将一系列数字转化为实际需要分配。你应该找到随机性不同的来源,要么学会正确的方式来生成所需的分发或使用可以为你做一个图书馆。 (0和N之间产生随机​​数例如,一个常用的方法是做兰特()%N ,但这不是真的是最好的方法。

Additionally rand() is generally not a very good random number generator and using it usually means you have to transform the series of numbers to the distribution you actually need. You should find a different source of randomness and either learn the proper ways to produce the distribution you want or use a library that can do it for you. (for example one common method of producing a 'random' number between 0 and N is to do rand() % N but this is not really the best method.

C ++提供了&LT一个更好的随机数库;随机&GT; 。它提供了不同的PRNG算法,如linear_congruential,mersennne_twister,甚至可能是加密安全的RNG(取决于实现)。它还提供了对象生产各种发行版,如unif​​orm_int_distribution的应避免的错误使兰特()%N

C++ provides a much better random number library in <random>. It provides different PRNG algorithms, such as linear_congruential, mersennne_twister, and possibly even a cryptographically secure RNG (depending on the implementation). It also provides objects for producing a variety of distributions, such as uniform_int_distribution which should avoid the mistake make in rand() % N.

这篇关于如何独特的是兰特(c)中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆