There isn’t a developer in the world who has never needed to use a random number generator of some kind. It’s in everything. You need it to simulate probabilty, create random animations, and the list goes on. I recently gave Xcode’s random number generator a thorough workout with my Snow Dice and Ski Dice apps. Every trick the apps generate uses multiple calls to this particular function. It’s incredibly useful.
Using arc4random().
Apple gives you an out-of-the-box random number generating function with Xcode. It’s called arc4random(). This function will generate any random number from zero up to the pre-defined maximum, which is 4294967296.
Now most of you won’t want to use arc4random() as is. You’ll want to use it to, say, pick a number between 1 and 10. To do that, you’ll tack on the modulus operator at the end. It looks like this.
NSInteger r = arc4random()%some_number;
So the following would give you any integer between zero and 3.
NSInteger r = arc4random%3;
In Snow Dice and Ski Dice, I use arc4random() to come up with a random number between 1 and some maximum. To do that, you just add one to the end of your function call.
This code gives you a random number between 1 and 3:
NSInteger r = arc4random%3 + 1;
Other uses of arc4random().
Xcode random number generation is the first thing you need to master if you want to simulate probability. Here’s a bit of code you could write to simulate the flip of a coin.
//The following bit of code simulates a coin flip using
//Xcode’s out-of-the-box random number generator
NSInteger r = arc4random()%2 + 1;
if ( r == 1) {
//Heads.
[coin setSide:heads];
return;
}
if ( r == 2 ) {
//Tails.
[coin setSide:tails];
return;
}
So you’re basically generating a random number between one and two. If the number is 1, then the coin is heads. If it’s 2, then the coin is tails. The probability for something like this is exactly 50% heads and 50% tails. It’s totally equal.
Weighting the coin.
But what if you wanted to load your dice? How might you go about adjusting the probabilities with the Xcode random number generator? Try out this code.
//This code creates a coin that will land on tails 90%
//of the time.
NSInteger r = arc4random()%10 + 1;
if ( r == 1) {
//Heads.
[coin setSide:heads];
return;
}
if ( r > 1 ) {
//Tails.
[coin setSide:tails];
return;
}
What does this do? First it generates a random number between one and ten. If that number is 1, we’ve got heads. But if it’s anything other than one, it’s tails.
Now 9 times out of 10, the random number won’t be one. It will be a number between 2 and 10. We’ve just weighted our coin. Tails occurs 90% of the time, and heads occurs just 10% of the time.
You can get as detailed with this as you want. If you want to give heads a 55% chance of happening, just change the range of the number generated up top to 100. Then any number less than our equal to 55 is heads and any number greater than that is tails. Easy.
Is arc4random() really random?
Is anything? I suppose we don’t really know, considering it’s not yet possible to take a complete snapshot of the Universe at any given moment in time. Having said that, random numbers in Xcode are not random in any real sense. arc4random() is what’s called a PRNG or pseudo-random-number-generator. It isn’t really random, but the program behind it is so complex that it looks random to most normal people.
The cool thing about arc4random() is that it never starts out at the same “random” number. Every time you start up your app, it comes up with a new random number. This is done via seeding. The first time you use arc4random(), it is automatically seeded with a number derived from the time and date you’re accessing the function. There’s so much variance in just a few seconds that you can practically guarantee you’ll never get the same sequence of random numbers over and over again.
Simulating probability is huge. It’s the only way I can keep skiers and snowboarders happy with the sorts of tricks Ski and Snow Dice come up with. Too much of one trick, and kids get bored. Too little of another, and they’re shaking for hours on end trying to get to their favorites. With arc4random(), I can tweak it to perfection. I’m sure you’ll find your uses too.
Like what you see in these tutorials? Got an iPhone app idea and need a developer? Contact me, and we’ll get started. If your idea is badass, I’m sure we’ll have an awesome business relationship.