What is a seed in terms of generating a random number?

44,098

Solution 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));

Solution 2

So, let's put it this way:

if you and your friend set the seed equals to the same number, by then you and your friend will get the same random numbers. So, if all of us write this simple program:

#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;
    }
}

We all will get the same random numbers which are (5, 8, 8).

And if you want to get different number each time, you can use srand(time())

Share:
44,098
SirRupertIII
Author by

SirRupertIII

Updated on December 26, 2020

Comments

  • SirRupertIII
    SirRupertIII over 3 years

    What is a seed in terms of generating a random number?

    I need to generate hundreds to thousands of random numbers, I have read a lot about using a "seed". What is a seed? Is a seed where the random numbers start from? For example if I set my seed to be 5 will it generate numbers from 5 to whatever my limit is? So it will never give me 3 for example.

    I am using C++, so if you provide any examples it'd be nice if it was in C++.

    Thanks!