Reprint: http://blog.csdn.net/kongfanyu/article/details/6387642 Computer There is no way to generate a real random number, is to use algorithm simulation, so you only call Rand, every time out something is the same. After a seed is set, different numbers can be produced depending on the seed. And how do you keep the seeds different? The simplest way of course is to use forever in the forward time. Srand (Time (0));//Set seed first
Rand ();//Then Generate random numbers Srand is a random number of seeds, you seed different seeds each time, the random number obtained by Rand is different. In order to plant a different seed every time, so choose time (0), when (0) is the current value (because the moment is different). Srand (Time (0)); is to give this algorithm a starting seed, that is, the random number of seeds of the algorithm, there is a number after this can produce random numbers,
Initializes the seed of the random number with the number of seconds since 1970.1.1.
Reference Case#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void Main (void)
{
int i;
Srand (Time (0));
/* Output 10 random numbers. */
for (i = 0; I < 10;i++)
printf ("%6d/n", Rand ());
} Output: 22614
15772
30620
24913
7556
31659
27876
27229
25083
16564
IOS Srand (Time (0))