All posts in iPhone App Development

Xcode Random Number Generation For iPhone Apps

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.

You may have realized I’ve been very very busy lately. What have I been doing? Growing my clientbase on Elance? Becoming the highest paid freelancer the world has ever known? Not exactly. I’ve been building an iPhone App, and now that it’s launched, I finally have some more time to get back to blogging.

Throughout this journey, I’ve learned a lot about building a business, things I wouldn’t have expected if you were to ask me about it just 6 months ago. I started with an idea, acted on it, got the right people involved, and then worked my ass for a few months straight. Now I have an app in the App Store that’s doing really well. I’n not at liberty to tell you how much I’m selling (can’t let the competition know), but we’re making headlines and sales.

The App Concept.

I’m a snowboarder. I love it. Out of everything I do on a snowboard, I love riding parks more than anything. It’s this constant challenge to improve upon your tricks and keep polishing them until they’re as stylish as you can make them. Oh, and it’s really really fun when you get your friends involved.

When we’re out riding, a lot of us play games with one another. We’re always pushing each other to try new stuff. Usually one of us calls out a trick for everyone to try. We’ll do this all day, keeping score and everything. At the end of the day, someone usually comes home with a new trick.

The problem with the game we were playing is that it’s heavily biased. The tricks you call out are usually the same tricks you call out all the time. Your mind always gets the best of you, and the desire to stay inside of your comfort zone usually wins out.

In an ideal world, you would challenge your friends with completely random tricks. At the time I came up with the app concept, there were these things called Sno Dice. They’re a set of physical dice that you roll. Each face has a different part of a trick on it, so you get completely random tricks from using them.

While Sno Dice are awesome, they’re kind of hard to use when you’re lapping the park super fast with your bros. For one, you have to find a surface to roll them on. You can generally do this, but most people don’t want to stop snowboarding to do it.

I came up with up the concept for the Snow Dice iPhone App while I was playing with physical Sno Dice. I kept thinking to myself, “this could be easier… a lot easier.”

Snow Dice, Ski Dice, and Skate Dice are the extension of the “random trick” concept into the iPhone world. The pocket format makes it really easy and much quicker to play the game. You don’t have to stop anymore. You can just shake your iPhone to roll the dice, get your trick, and try it.

Building the app.

I thought about making the app for a few months before I woke up one morning and just started on the thing. The first Snow Dice App was a prototype. It looked terrible, but it worked. I wrote the first bit of the source code entirely in Javascript, using Sencha Touch as the graphics framework. It was a decision I would later regret as I learned the hard way that Objective C and XCode really are the way to go.

The first version of Snow Dice was too complex for the public, but it was something I really wanted to use. That’s all that really mattered at the time. Instead of featuring just one jump trick and one rail trick, it automatically started with a series of three jump tricks. I was stoked about this because I enjoy learning new lines while out riding.

My biggest problem was a lack of graphics. I’m no graphics designer. I honestly don’t know where to start with it. So the first version was all text. Everything worked just fine. It was just really ugly.

Getting partners onboard.

For a few months, I had this app up on the App Store. It wasn’t selling very well, and I knew it needed something extra. I didn’t know what to do for a while until I approached someone who’s pretty well connected in the snowboarding industry down in New Zealand. That’s when the actual business started to form.

Here’s what a lot of beginners don’t understand. It’s not enough to build the app. A lot of people build apps. You need to get someone to help you market your app, and by that, I don’t mean someone who just talks the talk. You need someone with real connections in the industry you want to pursue. Without that, you have to rely on people discovering you. It’s totally hit and miss.

I had to give up a portion of my app company to my partner, and I don’t regret it at all. He’s got the connections. I don’t. That’s the way the world works. It’s better to accept a smaller portion of a bigger pie than it is to accept the entirety of a much smaller pie.

My business partner brought on a graphic designer, got a pro skier involved, got the app in the top ski and snowboarding websites, contributes to design and testing, and functions as a sounding board for new ideas. He also channels feedback from our users so my finger constantly stays on the pulse of what’s happening with the project.

To make anything really take off, you can’t just be a one man band. You need to assemble some kind of team. All four of us are responsible for making this thing awesome and ultimately getting it to sell.

Marketing the app.

It doesn’t matter if you’re selling socks or iPhone apps. You won’t have anything if you don’t have distribution. The App Store is a distribution channel all to itself, but it’s so crowded with other apps that it’s difficult to poke your head above the crowd. What you really need is someone in the industry pushing the concept. That’s why we got pro athletes involved.

People follow pro athletes. They pay attention to what pro athletes do. Pro athletes are really really good at presenting your product in the best possible light. With the right videographer, they make everything look as fun on video as it is in real life. Because of the pro athletes we got involved, we managed to grow our following by as much as 500%. That’s huge when you’re starting out small.

To market the app, we used a series of teaser videos. This is one of them. As you can see, it shows some of the fun possibilities of using the app while retaining that sense of mystery surrounding the product. We don’t explain what it does or what it is. We allow people to imagine it all on their own.

If you’ve just started building an app, you can begin to appreciate what this entire process involves. Do you have a nice camera? Do you know how to film like a pro? I don’t. That’s why I got other people involved. You’re nothing without your team.

Launching your app.

Launches need to be big or they don’t do anything. About a month before you actually launch, you have to start up a campaign to build anticipation for the app. If you don’t have media connections all on your own, you really won’t be able to do that. You need a marketer or you’re sunk before you even begin.

Our promoter setup a Facebook group dedicated to the app. We provided teaser videos before the app ever dropped. These are the things that got people excited prior to the release of the app. By the time we actually released it, everyone was so stoked about it that it sold a ton on the first night.

Now think about this. The App Store ranks apps by sales volume. If you get a huge spike, you can actually make it to the top 10 overnight. This happened with Snow Dice and Ski Dice. We continued to hold our position in the top spots, getting more sales and keeping our ranking. An app launch needs to be a big push or it doesn’t do anything at all for you.

Managing the incoming wave of user feedback.

I should have been celebrating on the eve of our launch, but that was probably one of the more stressful nights of my life. It’s the critical point when you start getting feedback from your users. People will find bugs, lots of them. Expect to spend the next few days cleaning everything up and preparing an update for the App Store.

With Ski Dice, we got a ton of awesome suggestions that went into effect almost immediately. I think people really liked to see such a high degree of responsiveness from us. This is where you show your true character as an app developer. Are you just in it for the money, or do you genuinely care? We care.

I’m stoked about what I got to build this summer. It’s probably one of the most fulfilling things I’ve ever done (aside from landing doubles). If you’re just starting, you’ve got all of my encouragement. It’s a challenging and sometimes stressful road, but don’t give up. When you find the right people, you can make it a real success.

It all started on a gondola ride with my friends. It was early season in Keystone, and we were playing with a pair of Sno Dice. My friend rolled the dice, and it came up with a new rail trick for us to try on Keystone’s rails. It was a pretty cool concept, but we felt it was ultimately too “messy” for the gondola ride and up top at the park.

Read more…

I'm Ted, a snowboarder by day and copywriter by night.