iOS Random Number Generator to a new view

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.

  • iOS - Share image + text to WhatsApp?
  • Fit width in iOS 8 Today Extensions
  • CoreTelephony crash
  • How to get user info from Facebook SDK in iOS?
  • Can't find keyplane that supports type 4 and 5 on iPhone 5s iOS 8.1.2 and Simulator 8.1
  • AFHTTPRequestOperation Cancel Request
  • Can anybody help me with the code?
    Thanks

    5 Solutions Collect From Internet About “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

    We can use the C function rand() for this:

    This generates an integer between 1 and 30. Alternatively you can use the arc4random function like this:

    int i = arc4random() % 30;
    NSLog(@"Random Number: %i", i);