srand
void rand ( unsigned int seed ); | stdlib.h |
cplusplus.com |
Initialize random number generator.
Uses seed parameter to set a new starting point for generating random numbers
with rand.
If seed is set to 1 the generator is reinitialized to its initial value
as before any call to rand or srand.
In order to generate true random numbers it is suggested to use as seed a value
that changes often, like the one returned by time function included
in <time.h> (the number of seconds elapsed since newyear 1970).
Parameters.
Return Value.
(none)
Portability.
Defined in ANSI-C.
Example.
/* rand/srand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main ()
{
/* initialize random generator */
srand ( time(NULL) );
/* generate some random numbers */
printf ("A number between 0 and 100: %d\n", rand()%100);
printf ("A number between 20 and 30: %d\n", rand()%10+20);
return 0;
}
Output:
A number between 0 and 100: 93
A number between 20 and 30: 21
See also.
rand