IOS Random Number

Source: Internet
Author: User
Tags random seed

IOS developers will certainly encounter random numbers. For the moment, there seems to be an arc4random (). So if you want to get a random number from 80 to 100, you can write it as int randomnumber.
=Arc4random ()+ 80

Int Index = Arc4random () % 6 + 1; is the random number from 1 to 6.There are also many other methods to obtain random numbers. On the Forum, users often discuss how to generate random numbers that are not repeated, that is, to generate a Random Seed, such as srand (Time (null ));

 Rand ()
% 1000;There is another such function round (random () + 10); the result is 10 ~ The number between 24, but a pseudo-random number.

How to obtain a random number between 0 and 1, excluding 0 and 1. The idea is: Calculate the random number between 1-100 and then/srand (time (0 ));

(Float) (RAND () % 100)/100;
(Float) (1 + arc4random ()/100 conclusion: it is best to use arc4random for random numbers, because Random Seed srandom (Time (null) must be set manually when random () is used )); otherwise, a pseudo-random number is generated. Some netizens have the following idea:   Nsarray * temp = [nsarray arraywithobjects: @ "1", @ "2", @ "3", @ "4", @ "5", @ "6 ", nil];

 Nsmutablearray * temparray = [[nsmutablearray alloc] initwitharray: temp];

 Nsmutablearray * resultarray = [[nsmutablearray alloc] init];

 Int I;

 Int COUNT = temp. count;

 For (I = 0; I <count; I ++ ){

 Int Index = arc4random () % (count-
I );

 [Resultarray addobject: [temparray objectatindex: Index];

 [Temparray removeobjectatindex: Index];

 }

 [Temparray release];

 Nslog (@ "resultarray
Is % @ ", resultarray );

In resultarray, The intvalue of each object is the random number you want.

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.