Objective-c of iOS development several different random number algorithms (ARC4RANDOM,RANDOM,SRANDOM)

Source: Internet
Author: User
Tags rand random seed

OBJECTIVE-C does not directly provide random data generation functions or methods, but we can directly use C's various random algorithms
The following is the use of several common random number algorithms, which can be referenced in the following header file
Stdlib.h

Arc4random
Random seeds are automatically generated at call time without the need for random seed
Returns a collection of [0,x]
int value = Arc4random ()% x;
Returns the collection of [1,x]
int value = (arc4random ()% x) + 1;


Random ()/rand ()
Does not use the seed random number to return any number within the Long/int range
Notice here the random returns long Rand returns int

Srandom (unsigned)/srand (unsigned)
Use random seed to return any number of arguments to Rand_max
Note that the Srandon here can use unsigned long as a parameter

It is to be noted that the RAND series random number uses a pseudo random algorithm, which is recommended to make different random seeds
For example:
Srand (Time (NULL))

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.