什么是产生一个随机数而言种子? [重复](What is a seed in terms of

2019-07-20 01:26发布

这个问题已经在这里有一个答案:

  • 什么是“播种”是什么意思? 4个回答

什么是产生一个随机数而言种子?

我需要产生成百上千个随机数的,我已经阅读了很多关于使用“种子”。 什么是种子? 就是随机数从开始种子? 例如,如果我把我的种子为5将它从5到任何我的极限是生成的数字? 所以,它永远不会给我3为例。

我使用C ++,因此,如果您提供的任何示例这会是很好,如果它是在C ++。

谢谢!

Answer 1:

What is normally called a random number sequence in reality is a "pseudo-random" number sequence because the values are computed using a deterministic algorithm and probability plays no real role.

The "seed" is a starting point for the sequence and the guarantee is that if you start from the same seed you will get the same sequence of numbers. This is very useful for example for debugging (when you are looking for an error in a program you need to be able to reproduce the problem and study it, a non-deterministic program would be much harder to debug because every run would be different).

If you need just a random sequence of numbers and don't need to reproduce it then simply use current time as seed... for example with:

srand(time(NULL));


Answer 2:

所以,让我们这么说吧:

如果你和你的朋友设置种子等于相同的号码,到那时你和你的朋友会得到相同的随机数。 所以,如果我们所有的人写这个简单的程序:

#include<iostream>
using namespace std;
void main () {
    srand(0);
    for (int i=0; i<3; i++){
        int x = rand()%11;          //range between 0 and 10
        cout<<x<<endl;
    }
}

我们都将获得相同的随机数中(5,8,8)。

如果你想每次都得到不同的号码,你可以用函数srand(时间())



文章来源: What is a seed in terms of generating a random number? [duplicate]