Srand ((unsigned) time (NULL)) detailed

Source: Internet
Author: User
Tags random seed

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);

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: 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.