随机数发生器 - 种子为什么每次 [英] Random number generator - why seed every time
问题描述
我是相对新的C和C ++。在Java中,我使用的语言进行编程中,其很容易实现的随机数生成。只需调用从名为Math类的静态随机方法。
I am relative new to c and c++. In java, the language I am used to program in, its very easy to implement random number generation. Just call the the static random-method from a class called Math.
int face = ((int)(Math.random() * 6) + 1);
的模拟一个骰子掷... 的
在C和C ++,你必须的种子随机数生成器的,通过调用函数srand功能
In c and c++ you have to "seed the random number generator" , by calling the srand-function
srand ( time(NULL) );
什么是做这点 - ?有我的意思是有每次code运行时种子随机数发生器的任何优势。
What is the point of doing this - I mean is there any advantage of having to seed the random number generator every time the code is run?
推荐答案
给定同样的种子,一个伪随机数发生器,每次将产生相同的序列。所以它归结为是否要伪随机数的不同的序列每次运行,还是不行。
Given the same seed, a pseudo random number generator will produce the same sequence every time. So it comes down to whether you want a different sequence of pseudo random numbers each time you run, or not.
这真的取决于你的需要。还有,当你要重复序列倍。当你做几次都不。您需要了解每一个具体应用的需求。
It really depends on your needs. There are times when you want to repeat a sequence. And times when you do not. You need to understand the needs of each specific application.
有一件事必须永远不会做代单个序列的过程中反复种子。这样做很可能会破坏你的序列分布情况。
One thing you must never do is seed repeatedly during generation of a single sequence. Doing so very likely will destroy the distribution of your sequence.
这篇关于随机数发生器 - 种子为什么每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!