iOS Random Number Generator to a new view

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.


You can use arc4random_uniform

NSUInteger r = arc4random_uniform(16);

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

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

    case 1:
    ...

}

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