The computer cannot generate real random numbers. Algorithm Simulation, so you only call Rand, each time the output is the same. After a seed is set, different numbers can be generated based on different seed values. But how can we ensure the seeds are different? The simplest way is to always move forward. Srand (time (0); // set the seed first
Rand (); // then generate a random number. srand is the number of random seeds. The number of seeds you plant varies with each time. The random number obtained with Rand is different. Time (0) is used to get the current time value (because the time is different every moment) for each different seed ). Srand (time (0 ));
It is to give this algorithm a startup seed, that is, the random seed number of the algorithm. Only after this number is available can random numbers be generated,
Initialize the random number seed in seconds from 1970.1.1 to the present.
Reference Cases
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>
void main (void)
{< br> int I;
srand (time (0 ));
/* output 10 random numbers. */
for (I = 0; I <10; I ++)
printf ("% 6D/N", Rand ());
} output result: 22614
15772
30620
24913
7556
31659
27876
27229
25083
16564
press any key to continue