iOS Random Number Generator to a new view

57,763

Solution 1

arc4random() is the standard Objective-C random number generator function. It'll give you a number between zero and... well, more than fifteen! You can generate a number between 0 and 15 (so, 0, 1, 2, ... 15) with the following code:

NSInteger randomNumber = arc4random() % 16;

Then you can do a switch or a series of if/else statements to push a different view controller:

UIViewController *viewController = nil;
switch (randomNumber)
{
    case 0:
        viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    break;
    // etc ...
}

[self.navigationController pushViewController:viewController animated:YES];

Or rather, upon rereading the question, it would look like the following:

UIViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" 
viewController.number = randomNumber;

And you'd have an NSInteger property on the MyViewController subclass.

Solution 2

You can use arc4random_uniform

NSUInteger r = arc4random_uniform(16);

Solution 3

According to Apple, the best way is to use arc4random_uniform and pass the upper bound:

arc4random_uniform(16)

From the docs:

arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/arc4random.3.html

Solution 4

    int randomIndex = arc4random() % 14 + 1 ; // gives no .between 1 to 15 ..

    switch (randomIndex)
{
    case 0 :
    push view 1 ;
    break;

    case 1:
    ...

}

Solution 5

In Swift 4.2, we don't have to call some "arc4random_uniform" function for creating random numbers, now we can just call a function "random(in:RANGE)".

//Create Random numbers Swift 4.2

//Int
let randomInt = Int.random(in: 1...10)

//Double
let radomDouble = Double.random(in: 1...10)

//Float
let randomFloat = Double.random(in: 1...10)
Share:
57,763

Related videos on Youtube

Sam
Author by

Sam

Updated on March 28, 2020

Comments

  • Sam
    Sam about 4 years

    I need some help with an app. I need to make a random number generator for integers between zero and fifteen, which will then, depending on which number is created, push to a view with the corresponding number. This is how I want it to work

    Push a button --> random number generator gives a number between 0 and 15 --> view pushes to another view that has been assigned the number that the random number generator gave.

    Can anybody help me with the code? Thanks

  • Admin
    Admin about 12 years
    or arc4random() % 16 to match the correct range, also arc4random() % 14 + 1 gives [1,14].
  • John Riselvato
    John Riselvato about 12 years
    why the -1? other then the % 14 + 1; I don't see anything wrong with this.
  • Victor Engel
    Victor Engel about 11 years
    The function arc4random_uniform() is preferred since it doesn't suffer from modulo bias.
  • avance
    avance about 10 years
    In 64 bit mode, arc4random_uniform returns a 32-bit int and NSUInteger is a 64-bit int, right?
  • Rémy Virin
    Rémy Virin about 10 years
    u_int32_t arc4random_uniform(u_int32_t /*upper_bound*/) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3); It looks like it's always an unsigned int on 32 bits.
  • Charles Chow
    Charles Chow almost 10 years
    Does this function generate psuedo random number? what seed does it use?
  • jk7
    jk7 over 7 years
    @CharlesChow quoting the man page, "The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3)."