# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>
Int main (void)
{
Int m, n;
Time_t t;
Srand (unsigned) time (& t); // The srand function is the initialization function of the random number generator. If not, the same random number is generated each time.
M = rand (); // generates an integer of 0-2 ^ 16
N = rand () % 10; // generates a random number ranging from 0 to 10.
Printf ("m = % d \ nn = % d \ n", m, n );
Return 0;
}
/*
Function Name: time header file: time. h function prototype: time_t time (time_t * timer)
Function: gets the current system time. The returned result is of the time_t type, which is actually a large integer,
The value indicates that the UNIX
The Epoch time of the system) the number of seconds from the current time point. Call localtime to convert the CUT time represented by time_t
It is the local time (we are in the + 8 zone, 8 hours more than the CUT) and converted to the struct tm type, each data member of this type
Indicates year, month, day, hour, minute, and second respectively.
*/
/* Www.2cto.com
The srand function is the initialization function of the random number generator.
Prototype: void srand (unsigned seed );
Usage: it needs to provide a seed, which corresponds to a random number. If the same seed is used
The rand () function returns the same random number. For example: srand (1); Use 1 directly to initialize the seed. However,
To prevent random numbers from being repeated, the system time is often used for initialization, that is, the time function is used to obtain the system time,
The returned value is the number of seconds that have elapsed since 00:00:00 GMT, January 1, 1970, and then
Convert time_t data to the (unsigned) type and then pass it to the srand function, that is, srand (unsigned) time (& t ));
There is also a frequently used variable that does not need to be defined in the time_t type, that is, srand (unsigned) time (NULL ));
Directly pass in a null pointer, Because t data is often not obtained through parameters in your program.
Srand (int) getpid (); Use the program ID (getpid () as the initialization seed. In the same program
Seed is fixed
*/
From the column of ainemo