How to Use Random Numbers in Your iPhone App

Matthew Campbell, January 4th, 2011

Would you like your iPhone app to be able randomly pick a number between 1 and 10 or to randomly select one string from a list?

Getting this done requires us to use the random function – a regular old C function. In this post, I am going to show you how to create an array of objects and then use a random number to select one object from the list.

FIrst, use the import statements to import these two libraries: stdlib and time. Put this code into the top of your file:

Now, create an array and populate it with strings that will serve as our objects:

Set the seed to the system clock. This will ensure that you get different results every time your run your app.

Here is how to get a random number – the number at the end of the statement indicates what the upper limit is. However, the function returns numbers starting with 0 so in practice you will get five results ranging from 0 to 4.

At this point we have the random number assigned to r and we can use it in any way we like. This is an example of using this number to pick an object our of our array.

As you can imagine, there are a lot of fun things that you could do with the random function: game logic, random thing of the week and so on.

What would you like to use the random function for in your app?

iOS is the new name for the operating system that runs the iPhone and now the iPad.  It really just stands for “operating system” which is something that all computers use to get things done.  The iPhone and iPad use iOS to get things done.

Programmers use iOS to make the apps that we all buy everyday.  It seems like you may need to be a rocket scientist or brain doctor to understand how to make apps but that is just not the case.  You simply need to learn how to give your iOS device instructions in a language that it understands.

I’m really excited to share that I have created a new email course along with a video tutorial series called Getting Started With iOS Development that will show you how to create your first iOS application.  You will also find out exactly what programming languages and technologies that you will need to learn to become a ninja mobile app developer.  And it’s free!

Yes it’s free and not only that but you will get a few more goodies that I am super-eager to share.  All you need to do to get this email course and video tutorial series is to stick your name and email address into the two boxes below and click the submit button:

Sign up to get immediate access to our Online Workshop, Getting Started With iOS Dev, free video tutorials and our app email course

Privacy Policy

[...] in Your iPhone App May 6, 2009, 12:13 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/05/how-to-use-random-numbers-in-your-iphone-app/) Would you like your iPhone app to be able randomly pick a number between 1 and 10 or to randomly [...]

How to Use Random Numbers in Your iPhone App…

You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

In my iKite app (http://ikiteapp.com) I use something similar to determine the speed and direction of the flying birds. That way they aren’t all doing the same thing.

Set the seed to the system clock. How do you do this??

[...] app to be able randomly pick a number between 1 and 10 or to randomly select one string from a list?This article explains how to use the randomize function with an array to do just that. Post a [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.