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

2
  • 2
    which code are you talking about ? Commented Mar 5, 2012 at 20:26
  • 1
    Here is a link regarding random number generation: stackoverflow.com/questions/160890/… Commented Mar 5, 2012 at 20:27

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The function arc4random_uniform() is preferred since it doesn't suffer from modulo bias.
Does this function generate psuedo random number? what seed does it use?
@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)."
19

You can use arc4random_uniform

NSUInteger r = arc4random_uniform(16); 

2 Comments

In 64 bit mode, arc4random_uniform returns a 32-bit int and NSUInteger is a 64-bit int, right?
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.
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

Comments

9
 int randomIndex = arc4random() % 14 + 1 ; // gives no .between 1 to 15 .. switch (randomIndex) { case 0 : push view 1 ; break; case 1: ... } 

2 Comments

or arc4random() % 16 to match the correct range, also arc4random() % 14 + 1 gives [1,14].
why the -1? other then the % 14 + 1; I don't see anything wrong with this.
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) 

Comments

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

Comments

0
extension CGFloat { static func random() -> CGFloat { return CGFloat(arc4random()) / CGFloat(UInt32.max) } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.