There are two main parts: generator rand (), seed setting srand ().
# Include <stdlib. h> int rand (void); void srand (unsigned int seed );
Using the same seed setting will generate the same pseudo-random sequence. The random numbers generated by Rand are between 0 and rand_max. Generally, the minimum value of rand_max is 32767 by default. However, rand_max in the stdlib. h library of GCC is
# Define rand_max 2147483647
Example:
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <time. h> 4 5 Int main (void) 6 {7 long time1; 8 int I, time2; 9 10 time1 = Time (null ); 11 printf ("time 1 = % LD \ n", time1); 12 13 time2 = (unsigned) time1/10; 14 printf ("time 2 = % LD \ n ", time2); 15 16 // set the starting point 17 srand (time2); 18 19 for (I = 0; I <10; ++ I) 20 {21 printf ("% d \ n", Rand (); 22} 23 return 0; 24}
Pseudo-random number generator