在 C++ 中使用 srand()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5620163/
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
use of srand() in c++
提问by Frustrated Coder
I am new to c++ so this doubt might look basic but I am not getting the difference between rand() and srand() and what do u mean by "seed" in srand()? when I write srand(time(NULL)), what does it do to generate random numbers, what does time(NULL) do here? and what is it? Thanks in advance
我是 C++ 新手,所以这个疑问可能看起来很基本,但我没有明白 rand() 和 srand() 之间的区别,你说 srand() 中的“种子”是什么意思?当我写 srand(time(NULL)) 时,它如何生成随机数,time(NULL) 在这里做什么?还有,这是什么?提前致谢
回答by deovrat singh
A random number generator requires a number(it is called seed) to generate random numbers . If the random number generator is given the same seed then every time it will generate the same sequence of random numbers . For example :-
随机数生成器需要一个数字(称为种子)来生成随机数。如果随机数生成器被赋予相同的种子,那么它每次都会生成相同的随机数序列。例如 :-
If you run the program and it is generating random sequence 2,78,45,60 . If second time you run the program you will again get the same sequence 2,78,45,60.
如果您运行该程序并且它正在生成随机序列 2,78,45,60 。如果您第二次运行该程序,您将再次获得相同的序列 2,78,45,60。
srand function is used to change the seed of the random number generator.By setting srand(time(NULL)) , you are setting the seed of the random number generator to the current time.By doing this every time you run the program you will get different random sequences :-
srand 函数用于更改随机数生成器的种子。通过设置 srand(time(NULL)) ,您将随机数生成器的种子设置为当前时间。每次运行程序时都这样做,您将获得不同的随机序列:-
For example for the first run if you are getting 2,78,45,60 . Next time you might get 5,3,6,80 (depending on the current time,as seed has been changed since the time has changed since the last run)
例如对于第一次运行,如果您得到 2,78,45,60 。下次您可能会得到 5,3,6,80(取决于当前时间,因为自上次运行以来时间已更改,种子已更改)
for more info refer these :-
有关更多信息,请参阅这些:-
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
回答by sharptooth
rand()
doesn't produce a random number - it uses some quite easy formula to compute the next "random value" based on its stored internal state that changes each time a random value is generated. srand()
sets that internal state.
rand()
不产生随机数 - 它使用一些非常简单的公式根据其存储的内部状态计算下一个“随机值”,每次生成随机值时都会发生变化。srand()
设置该内部状态。
This way you can get reproduceable sets of numbers - you call srand()
with a given value and rand()
then produces a set of values. When you start the program next time and call srand()
with exact same value rand()
will produce exactly the same set of values. This is useful for simulation.
通过这种方式,您可以获得可复制的数字集 - 您srand()
使用给定值进行调用,rand()
然后生成一组值。当您下次启动程序并srand()
使用完全相同的值调用时,rand()
将产生完全相同的一组值。这对于模拟很有用。
Calling srand( time( NULL ) )
makes your program generate a set of values that will depend on the current time and therefore be irreproduceable - each time you restart a program a new set of numbers is generated.
调用srand( time( NULL ) )
使您的程序生成一组取决于当前时间的值,因此是不可复制的 - 每次重新启动程序时都会生成一组新的数字。