Three random number methods in iOS and three random numbers in ios

Source: Internet
Author: User
Tags random seed

Three random number methods in iOS and three random numbers in ios

Ios has the following three random number methods:

// The first srand (unsigned) time (0); // the random number generated each time without adding this sentence does not change int I = rand () % 5; // srandom (time (0); int I = random () % 5; // The third int I = arc4random () % 5;

Note:

Rand ()AndRandom ()Actually, it is not a real pseudo-random number generator.InitializationRandom Seed. Otherwise, the random number generated each time is the same.

Arc4random ()It is a real pseudo-random algorithm and does not need to generate random seeds, because it is automatically generated during the first call. And the range is twice that of rand. In the iPhone, RAND_MAX is 0x7fffffff (2147483647), while the maximum value returned by arc4random () is 0x100000000 (4294967296 ).

 

PrecisionComparison: arc4random ()> random ()> rand ()

 

Appendix: set of common arc4random () Methods

// Obtain a random integer in the range of [0,100) 0, excluding 100int x = arc4random () % 100; // obtain a random number in the range ), including 500, excluding 1000int y = (arc4random () % 501) + 500; // obtain a random integer in the range of [from, to), including from, not including to-(int) getRandomNumber :( int) from to :( int) to {return (int) (from + (arc4random () % (to-from + 1 )));}

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.