The Srand function is the initialization function of the random number generator. Prototype: void Srand (unsigned seed);
Usage: It initializes a random seed that provides a seed that corresponds to a random number, and if the rand () function following the same seed is used, the same random number appears, such as: Srand (1); The seed is initialized with a direct use of. However, in order to prevent random numbers from repeating every time, the system time is often used to initialize, that is, the time function is used to obtain the system times, its return value is from 00:00:00 GMT, January 1, 1970 to the current number of seconds, and then convert time_t data to (unsigned ) type is then passed to the Srand function, namely: Srand ((unsigned) time (&t)); There is also a common usage that does not need to define the time_t type T variable, namely: Srand ((unsigned) time (NULL)); Pass in a null pointer directly, because your program often does not need the data obtained by the parameter.
Further explanation: The computer does not produce a real random number, instead, some of the irregularly arranged numbers are stored in the computer, dividing them into several equal n parts, getting the number for each plus a number with a srand () function, and then rand () Gets the numbers in order, When the parameter values of Srand () are fixed, the number of rand () is also fixed, so the general Srand parameter is time (NULL), because the system has been changing, so the number of rand () has been changing, equivalent to random number. As long as the user or third party does not set random seeds, then by default the random seed comes from the system clock. If you want to generate a sequence of random numbers in a program, you need to set up a random seed at most before generating a random number. That is, simply call Srand ((unsigned) time (NULL) at the beginning of the main program; You can use Rand directly behind it. Do not place Srand ((unsigned) time (NULL)) in a for-wait loop;
void Test_rand (void)
{
unsigned long n;
Srand ((unsigned) time (NULL));
for (int i = 0; i < i++)
{
n = rand ();
printf ("d\n", n);
}
}