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

SirRupertIII picture SirRupertIII · Feb 16, 2013 · Viewed 42.4k times · Source

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!

Answer

6502 picture 6502 · Feb 16, 2013

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