From: http://www.gnu.org/s/hello/manual/libc/ISO-Random.html
Function: void srand (unsigned int seed)
This function establishes seed as the seed for a new series of pseudo-random numbers. If you call rand before a seed has been established with srand, it uses the value 1 as a default seed.
If you provide the same seed in srand
, you will always get the same sequence of numbers. If you never call srand
, then you will always get the same sequence every time you run your application.
A common trick to seed rand
is to used time(0)
- basically read the system's clock. This is fine in a simple application that just needs to be "mostly random".
But, when true randomness really is important:
Beware that you shouldn't simply seed from the system time in an application that must be cryptographically secure (e.g. something doing authentication hash calculations), or have a strong guarantee of randomness (e.g. a gambling game for real money).
In fact, you shouldn't use rand
at all in such an application. Instead, you should use a different random function; possibly one that is OS specific (specially provided for cryptography), or use a true physical source of random numbers.