Start a new sequence of pseudo-random integers
#include <stdlib.h> void srand( unsigned int seed );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The srand() function uses the argument seed to start a new sequence of pseudo-random integers to be returned by subsequent calls to rand(). A particular sequence of pseudo-random integers can be repeated by calling srand() with the same seed value. The default sequence of pseudo-random integers is selected with a seed value of 1.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main( void ) { int i; srand( 982 ); for( i = 1; i < 10; ++i ) { printf( "%d\n", rand() ); } /* Start the same sequence over again. */ srand( 982 ); for( i = 1; i < 10; ++i ) { printf( "%d\n", rand() ); } /* Use the current time as a seed to get a different sequence. */ srand( (int) time( NULL ) ); for( i = 1; i < 10; ++i ) { printf( "%d\n", rand() ); } return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |