Rand () generates random numbers.

Source: Internet
Author: User
Tags epoch time

# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.