C++ rand() 在应用程序重启时总是返回相同的序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13274338/
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
rand() always returns the same sequence on application restart
提问by Matthew
I have the following method which generates a random number:
我有以下生成随机数的方法:
int random_number() //Random number generator
{
int x = rand() % 1000000 + 1; //Generate an integer between 1 and 1000000
return x;
}
The call to this method is used in a loop which iterates five times. The problem with this method is that it always seems to generate the same numbers when running the program several times. How can this be solved?
对该方法的调用在一个循环中使用,该循环迭代了五次。这种方法的问题在于,在多次运行程序时,它似乎总是生成相同的数字。如何解决这个问题?
回答by femtoRgon
You need to seedthe random number generator, such as:
您需要播种随机数生成器,例如:
srand ( time(NULL) );
int x = rand() % 1000000 + 1;
Seeding the pseudorandom number generator essentially decides on the random number set that it will iterate through. Using the time is the standard method of achieving adequately random results.
为伪随机数生成器设置种子基本上决定了它将迭代的随机数集。使用时间是获得充分随机结果的标准方法。
EDIT:
编辑:
To clarify, you should seed only once and get many random numbers, something like this:
澄清一下,您应该只播种一次并获得许多随机数,如下所示:
srand ( time(NULL) );
loop {
int x = rand() % 1000000 + 1;
}
Rather than something like:
而不是像:
loop {
//Particularly bad if this line is hit multiple times in one second
srand ( time(NULL) );
int x = rand() % 1000000 + 1;
}
回答by tomahh
make a call to srand(time(NULL));
when your program launches.
srand(time(NULL));
在您的程序启动时调用。
srand
sets a seed to the rand function. Giving it the return value of time(NULL)
helps getting a different seed at each program run.
srand
为 rand 函数设置一个种子。给它返回值time(NULL)
有助于在每次程序运行时获得不同的种子。
As you tagged your question as c++, you could either use c++11 feature to handle randomnumber generation.
当您将问题标记为 c++ 时,您可以使用 c++11 功能来处理随机数生成。
回答by gda2004
femtoRgon is right. This will seed the program but take a look at the new c++ standard as they have improved random number generation see
femtoRgon 是对的。这将为程序提供种子,但请查看新的 c++ 标准,因为它们改进了随机数生成,请参阅
回答by Paul Rubel
rand is not really a random number, but rather a pseudo-random one that just "looks" random if you don't know the algorithm used to generate the values. From the man page:
rand 并不是一个真正的随机数,而是一个伪随机数,如果您不知道用于生成值的算法,它只是“看起来”随机的。从手册页:
The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive
rand() 函数返回 0 到 RAND_MAX 范围内的伪随机整数
pseudo-random means that given the same input, called a seed, it will give the same output. This is actually quite useful when you're trying to debug a problem since the same "random" values will be returned, letting you reproduce the problem. It's bad if you really need randomness.
伪随机意味着给定相同的输入,称为种子,它将给出相同的输出。当您尝试调试问题时,这实际上非常有用,因为将返回相同的“随机”值,让您重现问题。如果你真的需要随机性,那就不好了。
As noted above by changing the seed to something different on each run, like say seconds since the epoch, you can get different values from your call to rand().
如上所述,通过在每次运行时将种子更改为不同的内容,例如自纪元以来的秒数,您可以从对 rand() 的调用中获得不同的值。
srand(time(NULL))
If you're trying to debug you may want to print out the seed so you can reproduce problems if they occur.
如果您正在尝试调试,您可能需要打印出种子,以便在出现问题时重现问题。