27

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

Sam
  • 799
  • 2
  • 8
  • 14
  • 2
    which code are you talking about ? –  Mar 05 '12 at 20:26
  • 1
    Here is a link regarding random number generation: http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c – Jeremy Mar 05 '12 at 20:27

7 Answers7

97

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.

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
  • 8
    The function arc4random_uniform() is preferred since it doesn't suffer from modulo bias. – Victor Engel May 06 '13 at 18:26
  • Does this function generate psuedo random number? what seed does it use? – Charles Chow Jun 08 '14 at 00:25
  • @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)."` – jk7 Jan 05 '17 at 18:50
19

You can use arc4random_uniform

NSUInteger r = arc4random_uniform(16);
Rémy Virin
  • 3,379
  • 23
  • 42
  • In 64 bit mode, arc4random_uniform returns a 32-bit int and NSUInteger is a 64-bit int, right? – avance Apr 11 '14 at 19:41
  • 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. – Rémy Virin Apr 12 '14 at 06:52
12

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

frodo2975
  • 10,340
  • 3
  • 34
  • 41
9
    int randomIndex = arc4random() % 14 + 1 ; // gives no .between 1 to 15 ..

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

    case 1:
    ...

}
Shubhank
  • 21,721
  • 8
  • 65
  • 83
1

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)
0

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);
Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
0
extension CGFloat {
   static func random() -> CGFloat {
       return CGFloat(arc4random()) / CGFloat(UInt32.max)
   }
}
Bhargav Sejpal
  • 1,352
  • 16
  • 23