IOS-Random Number

Source: Internet
Author: User
Tags random seed

IOS-Random Number

Functions such as rand (), srand (), random (), and arc4random () are provided in C.
Use arc4random to generate a random number
1.1 obtain a random integer in the range of [0,100) 0, excluding 100

int x = arc4random() % 100;

1.2 obtain a random number in the range of [100,200], including 100, including 200

int y =100 +  (arc4random() % 101);

1.3 Extraction Method

Returns a random integer in the range of [from, to], including from, including

-(int)getRandomNumber:(int)from to:(int)to    {      return (int)(from + (arc4random() % (to – from + 1)));    }

Use rand () to generate a random number
Note:

In the standard C library, the function rand () can generate 0 ~ A random number between RAND_MAX. RAND_MAX is an integer defined in stdlib. h, which is related to the system.
The rand () function does not have any input parameter and is directly referenced by the expression rand;

printf("Random numbers are: %i %i\n",rand(),rand());

Pseudo-Random Number:
Because the rand () function generates integers in the specified order, the same two values are printed each time the preceding statement is executed. Therefore, the random C language is not really random, it is also called a pseudo-random number.
To make the program generate a random value of a new sequence during each execution,
We usually provide a new random seed for the random number generator. The srand () function (from stdlib. h) can spread seeds for the random number generator. As long as the seeds are different, the rand () function will generate different random number sequences.
Srand () is called the initialization tool of the random number generator.
Use rand () to randomly generate an integer in [x, y]

/* K is the number of randomly generated values in the desired range. The result of rand () % a is the maximum value of A-1 */int k = x + rand () % (y-x + 1)

Comparison
Rand () and random () are not really pseudo-random number generators. before using them, initialize random seeds. Otherwise, the random numbers generated each time are the same.

Arc4random () is a real pseudo-random algorithm that does not need to generate random seeds, because it is automatically generated when it is called for the first time. 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 ).

Precision comparison: arc4random ()> random ()> rand ().

// 1. rand (), without the random number generated each time srand (unsigned) time (0); int I = rand () % 100; // 2. random () srandom (time (0); int I = random () % 100; // 3. arc4random () int I = arc4random () %100;

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.