C Reference function srand() initialize random number generator
This function of stdlib will initialize the random number generator that can be used with the rand() function.
Usage of srand():
void srand ( unsigned int seed );
The function srand() is used to initialize the pseudo-random number generator by passing the argument seed.
Often the function time is used as input for the seed.
If the seed is set to 1 then the generator is reinitialized to its initial value. Then it will produce the results as before any call to rand and srand.
Parameters:
An integer value to initialize the random number generator. Often the function time is used as input for the seed.
Return value:
There is nothing returned by srand.
Source code example of srand():
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
{
printf ("Our first number: %d\n", rand() % 100);
srand ( time(NULL) );
printf ("Some random number: %d\n", rand() % 100);
srand ( 1 );
printf ("The first number again: %d\n", rand() %100);
return 0;
}
Output of the srand example program above:
Our first number: 41
Some random number: 4
The first number again: 41