Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Can anybody help me with the code? Thanks

like image 702
Sam Avatar asked Mar 05 '12 20:03

Sam


People also ask

Can you manipulate a number generator?

With some random number generators, it's possible to select the seed carefully to manipulate the output. Sometimes this is easy to do. Sometimes it's hard but doable. Sometimes it's theoretically possible but practically impossible.

Is there a random number generator app?

Random: All Things Generator is a random number generator that sports a clean interface and is compatible with both Android and iOS devices. The app is remarkably easy to use as you can generate new random numbers using its 'randomize button'. The randomizer can generate both random numbers and letters.


1 Answers

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.

like image 77
Ash Furrow Avatar answered Oct 11 '22 04:10

Ash Furrow