|
发表于 2003-12-7 16:30:30
|
显示全部楼层
srand() and rand()
example:
#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;
} |
|