srand()
QNX SDP8.0C Library ReferenceAPIDeveloper
Start a new sequence of pseudo-random integers
Synopsis:
#include <stdlib.h>
void srand( unsigned int seed );
Arguments:
- seed
- The seed of the sequence of pseudo-random integers.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
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.
Examples:
#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;
}
Classification:
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Yes |
Thread | Yes |
Page updated: