Making random numbers in ObjectiveC, the quick and dirty way.

This is another part of my iphone development experience, where I really feel the pain over and again. So putting up my quick way to do get a random number. There are a lot of methods regarding the random number generation in objectiveC and there are even third party attempts to get the best random number possible.
The three most obvious functions are rand(),random() and arc4random(). While a lot can be heard around of not getting a random number by the first two functions, the last options is better. At least I get decent random numbers with it.
Now to get a random number between 2 points, the code would be

//random number between 0 and 5 not 5, till 4
int i=arc4random()%5;
//random number between 10 and 15 not 15, till 14
int j=10 + arc4random()%5;

Hope that makes my next random number creation search leads me to look at this post.
Happy coding :)

One thought on “Making random numbers in ObjectiveC, the quick and dirty way.

  1. Thanks! I found that random() was NOT at all giving random output. It even gave an output which matched the textbook example I was using!

Comments are closed.