IOS: srand () and rand (), iossrandrand

Source: Internet
Author: User
Tags random seed

IOS: srand () and rand (), iossrandrand

Srand (seed) is used to set the starting point of a random sequence to seedsrand (int) time (0), which indicates that the int value corresponding to the current time is the starting point of the random sequence, so that each program runs, the time function of different random numbers can be obtained because of different starting points. The time function provides the number of seconds since 00:00:00, January 1, January 1, 1970. It must contain a parameter to store this number of seconds. time () may cause syntax errors, time (0) indicates that the storage is not performed in seconds.

00:00:00 is actually ----> the program developer wrote the first draft in X for the unix C language, so this zero point is set. The popularity of computers and PCs is also a concern after 1970s. So, don't worry about why it starts at this point in time.

Srand (int) time (0); // In fact, the random number generated by rand is always different.

Srand (time (0); // set the seed first
Rand (); // then generate a random number
Srand is the number of random seeds. Each time you plant a different seed, the random number obtained using Rand is different. Time (0) is used to get the current Time value (because the Time is different every moment) for each different seed ). 
Srand (time (0 )); 
It is to give this algorithm a startup seed, that is, the random seed number of the algorithm. Only after this number is available can random numbers be generated, 

Initialize the random number seed in seconds from 1970.1.1 to the present.
 
 

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.