C语言 srand(time(NULL)) 改变种子值的速度不够快
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
srand(time(NULL)) doesn't change seed value quick enough
提问by Chris
I have written a simple random number generator in C. int lis the lower bound and int uis the upper bound.
我用 C 写了一个简单的随机数生成器。int l是下界,int u是上限。
It works just fine, however I have a question regarding seeding it. If I was to run this in a loop, time(NULL)doesn't change the seed value quick enough to be able to prevent getting a consecutive series of random numbers that are exactly the same.
它工作得很好,但是我有一个关于播种它的问题。如果我要在循环中运行它,time(NULL)则不会足够快地更改种子值以防止获得完全相同的连续随机数系列。
I'm wondering how anybody else might have approached this problem. All the examples I've found online use time(NULL)as the seed value generator.
我想知道其他人是如何解决这个问题的。我在网上找到的所有示例都time(NULL)用作种子值生成器。
int generateRandom(int l, int u)
{
srand(time(NULL));
int r = rand() % ((u - l) + 1);
r = l + r;
return r;
}
If I was to run these lines of code right next to each other, both Rand1and Rand2would be exactly the same.
如果我是紧挨着彼此碰撞的几行代码,无论是Rand1和Rand2是完全一样的。
printf("Rand1 = %d\n", generateRandom(10, 46));
printf("Rand2 = %d\n", generateRandom(10, 46));
回答by Andrew Cooper
srand(time(NULL))should be run exactly once to intialise the PRNG. Do this in Main when the application starts.
srand(time(NULL))应该只运行一次以初始化 PRNG。在应用程序启动时在 Main 中执行此操作。
Explanation:
解释:
A PRNG (Pseudo-Random Number Generator) generates a deterministic sequence of numbers dependent on the algorithm used. A given algorithm will always produce the same sequence from a given starting point (seed). If you don't explicitly seed the PRNG then it will usually start from the same default seed every time an application is run, resulting in the same sequence of numbers being used.
PRNG(伪随机数生成器)根据所使用的算法生成确定性的数字序列。给定的算法将始终从给定的起点(种子)产生相同的序列。如果您没有明确地为 PRNG 设置种子,那么它通常会在每次运行应用程序时从相同的默认种子开始,从而导致使用相同的数字序列。
To fix this you need to seed the PRNG yourself with a different seed (to give a different sequence) each time the application is run. The usual approach is to use time(NULL)which sets the seed based on the current time. As long as you don't start two instances of the application within a second of each other, you'll be guaranteed a different random sequence.
要解决此问题,您需要在每次运行应用程序时自己为 PRNG 设置不同的种子(以提供不同的序列)。通常的方法是使用time(NULL)which 根据当前时间设置种子。只要您没有在一秒钟内启动应用程序的两个实例,您就可以保证不同的随机序列。
There's no need to seed the sequence each time you want a new random number. And I'm not sure about this, but I have the feeling that depending on the PRNG algorithm being used re-seeding for every new number may actually result in lower randomness in the resulting sequence.
每次您想要一个新的随机数时,都无需为序列设定种子。我不确定这一点,但我有一种感觉,根据所使用的 PRNG 算法,为每个新数字重新播种实际上可能会导致结果序列的随机性较低。
回答by jonsca
Seed once at the beginning of main. If you reseed too quickly during the same second, you will end up getting the same numbers.
在主要开始时播种一次。如果您在同一秒内重新播种太快,您最终将获得相同的数字。
回答by user2608735
srand( (unsigned) time(NULL) * getpid());
srand( (unsigned) time(NULL) * getpid());
yields a more diverse random set (on OSX 10.8), including on short cycle tests.
产生更多样化的随机集(在 OSX 10.8 上),包括短周期测试。
回答by Sami J. Lehtinen
Don't seed it every time, only at the beginning of your program.
不要每次都播种,只在程序开始时播种。
Also, many books advice against using the C-lib standard random functions. If you need good pseudo-random numbers, there's a good algorithm in Press et al., Numerical Recipes, 3rd. Edition.
此外,许多书籍建议不要使用 C-lib 标准随机函数。如果你需要好的伪随机数,Press et al., Numerical Recipes, 3rd 中有一个很好的算法。版。
回答by Ragnar123
I assume you call the generateRandomfunction from another function, mainor something.
我假设您generateRandom从另一个函数main或其他函数调用该函数。
If you declare the seed inside the function, you'll reset the function. Resetting the function, will make the same numbers appear several times, the same second.
如果您在函数内声明种子,您将重置该函数。重置该功能,会使相同的数字在同一秒内出现多次。
Moving srand(time(NULL));to main function will solve the problem.
移动srand(time(NULL));到 main 函数将解决问题。
回答by Henrique Rocha
If you are using different processes, use (rand()+getpid())%range;I use it to test the same program with random values many times a second (If you make rand numbers, exit the program and run again very fast, the numbers will be the same)
如果您使用不同的进程,请使用(rand()+getpid())%range;我用它每秒多次用随机值测试同一个程序(如果您生成 rand 数字,请退出程序并再次快速运行,数字将相同)

