C++ 随机数生成器 - 为什么每次都产生种子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20636859/
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
Random number generator - why seed every time
提问by user2991252
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.
我对 c 和 c++ 比较陌生。在我用来编程的语言 java 中,它很容易实现随机数生成。只需从名为 Math 的类中调用静态随机方法即可。
int face = ((int)(Math.random() * 6) + 1);
simulates a dice-throw ...
模拟掷骰子...
In c and c++ you have to "seed the random number generator", by calling the srand-function
在 c 和 c++ 中,您必须通过调用 srand 函数来“播种随机数生成器”
srand ( time(NULL) );
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?
这样做有什么意义 - 我的意思是每次运行代码时都必须为随机数生成器设置种子有什么好处吗?
回答by David Heffernan
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.
您绝对不能做的一件事是在生成单个序列期间重复播种。这样做很可能会破坏序列的分布。
回答by dasblinkenlight
What's usually called a random number generator is actually a pseudo-randomnumber generator. This typically means that you can generate the same random sequence if you provide the "key" to that sequence, referred to as the "seed". This is very useful when you wish to test your algorithm that is based on randomization, and you need to ensure repeatable results.
通常所说的随机数生成器实际上是一个伪随机数生成器。这通常意味着如果您提供该序列的“密钥”(称为“种子”),您就可以生成相同的随机序列。当您希望测试基于随机化的算法并且需要确保可重复的结果时,这非常有用。
If you do not "seed" your Random number generator, it is seeded with some(usually based on system time) random number by default, and therefore produces the different sequence every time that you run your program.
如果您没有“播种”随机数生成器,默认情况下它会使用一些(通常基于系统时间)随机数进行播种,因此每次运行程序时都会产生不同的序列。
回答by Some programmer dude
If you don't seed the generator, it will have the same seed every time you run your program, and the random number sequence will be the same each time.
如果不为生成器设置种子,则每次运行程序时它都会有相同的种子,并且每次的随机数序列都相同。
Also note that you only should to seed the generator once, at the beginning of the program.
另请注意,您只应在程序开始时为生成器设置一次种子。
回答by Klas Lindb?ck
The advantage is that you can repeat a random number sequence by supplying the same seed.
优点是您可以通过提供相同的种子来重复随机数序列。
The game Elite used this to store an entire world, consisting of thousands of stars, as a single number. To generate the exact same world a second time, the just supplied the same seed.
游戏精英使用它来存储整个世界,由数千颗星星组成,作为一个数字。为了第二次生成完全相同的世界,刚刚提供了相同的种子。
回答by haccks
The seed is needed for pseudo random number generator to generate different random number sequence from previous ones (not always). If you do not want the repeated sequence then you need to seed the pseudo random number generator.
伪随机数生成器需要种子来生成与以前不同的随机数序列(并非总是如此)。如果您不想要重复序列,则需要为伪随机数生成器设置种子。
Try these codes and see the difference.
Without seed:
试试这些代码,看看有什么不同。
无种子:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d", rand()%6 + 1);
}
With seed:
带种子:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
printf("%d", rand()%6 + 1);
}
回答by Fiddling Bits
In C/C++, a dice roll would be simulated like this:
在 C/C++ 中,掷骰子会像这样模拟:
int face = (rand() % 6) + 1);
^
|___________ Modulo operator
The % 6
limits the random number to 0 through 5and the + 1
is made to offset the limit, so it becomes 1 through 6.
The% 6
将随机数限制为0 到 5并且+ 1
用于抵消限制,因此它变为1 到 6。
回答by GMasucci
The random number generator is not truly random: say you seed it with 12 and make 100 random numbers, repeat the process and seed it with 12 again and make another 100 random numbers, they will be the same.
随机数生成器并不是真正的随机数:假设您用 12 种子生成 100 个随机数,重复该过程并再次用 12 生成种子并生成另外 100 个随机数,它们将是相同的。
I have attached a small sample of 2 runs at 20 entries each with the seed of 12 to illustrate, immediately after the code which created them:
我在创建它们的代码之后立即附上了 2 个运行的小样本,每个运行 20 个条目,每个条目的种子为 12 个以说明:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
srand(12);
for (int i =0;i<100; i++)
{
cout << rand() << endl;
}
return 0;
}
To avoid such repetition it is commonplace to use a more unique value, and as the time is always changing, and the chances of a two programs generating random sequences at exactly the same time are slim (especially when at the millisecond level), one can reasonably safely use time as an almost unique seed.
为了避免这种重复,使用更独特的值是司空见惯的,因为时间总是在变化,两个程序同时生成随机序列的机会很小(尤其是在毫秒级别时),可以合理安全地使用时间作为几乎独特的种子。
Requirements: seeding only need take place once per unique random sequence you need to generate.
要求:每个您需要生成的唯一随机序列只需要进行一次播种。
However, there is an unexpected up-/down-side to this: if the exact time is known of when the first sequence is generated then the exact sequence can be re-generated in the future by entering the seed value manually, causing the random number generator to step through its process in the same fashion as before (this is an upside for storing random sequences, and a downside for preserving their randomness).
然而,这有一个意想不到的上行/下行:如果第一个序列生成的确切时间是已知的,那么将来可以通过手动输入种子值来重新生成确切的序列,从而导致随机数字生成器以与以前相同的方式逐步完成其过程(这是存储随机序列的优点,也是保留其随机性的缺点)。