The Code that Picked the Contest Winner

Matthew Campbell, May 18th

As some of you may know, as part of the promotion for my new eBook, How to Make an iPhone App, I decided to give away a free copy to people who signed up for my early notification list.

Of course, I needed to find a way to pick the winner of both of the contests I promised. I thought it would be fun to adapt the randomization code I demonstrated a few weeks ago in this blog to pick the winner of the first contest.

Basically, what we are going to do is use the C functions to pick a number from 1 to 111 (111 is the number of people who signed up for the early notification list last week).

The first thing I need to do is to import the C libraries that I am going to need. So I type (actually copy and paste) this into my app delegate’s interface file:

#import "stdlib.h"
#import "time.h"

The I set the seed from the system clock so we know the results will be unpredictable. Just a note – if you are running tests and do not want a different number to come up each time leave this statement out.


Now I need to actually 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 to the number you typed in minus one. If I had just put 111 in the code below the random number would be in the range 0 to 110 which is not what I want. I do not have a zero position in my email list.

So, I am going to add 1 to the integer so that the function returns a number between 1 and 111.

int positionOfLuckyWinnerInMyMailingList = random() % 111 + 1;

Now, I am going to insert this number into a string with a rousing comment:

NSString *winnerIs = [NSString stringWithFormat:@"and the WINNER is: Number %i", positionOfLuckyWinnerInMyMailingList];

Now I am going to use an alert to use the string above to announce the winner. Of course, since I hate writing alert code over and over again I have created a function to use an alert with one line of code. A while back I wrote an article about why and how I do this if you are interested check it out here.

To use my function I needed to import the file I have my alert code located in:

#import "ASFunctions.h"

“AS” stands for App Shop, my iPhone company. A lot of Objective-C & Cocoa developers like to prefix their frameworks with the initials of their organization.

Next, I use a global function to shout out the name (actually the position in my email list) of the winner.

[ASFunctions alert: winnerIs];

Here is the code in its entirety

#import "TipOfTheWeekAppDelegate.h"
#import "stdlib.h"
#import "time.h"
#import "ASFunctions.h"

@implementation TipOfTheWeekAppDelegate
@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {


int positionOfLuckyWinnerInMyMailingList = random() % 112 + 1;

NSString *winnerIs = [NSString stringWithFormat:
@"and the WINNER is: Number %i",

[ASFunctions alert: winnerIs];

[window makeKeyAndVisible];


- (void)dealloc {
[window release];
[super dealloc];



Now, all I need to do is run the app and see who wins:

Congrats #45!

Now, I need to go and see who that is on my Aweber mailing list. I run into a little snag – Aweber doesn’t list the mailing list clients with a number. So, I have to download the whole list into “Numbers”, the Apple spreadsheet, on my desktop to find out who the number corresponds to. Turns out to be Mr. Bill [Redacted]. Sorry, I cannot actually release people’s names or email – you will have to trust me that Bill will get this free copy.

Awesome, I a few minutes I will email Bill to let him know that we won the copy of the book.

Thanks for your attention – hope this was not too much inside baseball for you!

[...] the Contest Winner May 18, 2009, 1:42 pm Filed under: Uncategorized (My Original Blog Post: As some of you may know, as part of the promotion for my new eBook, How to Make an iPhone App, I [...]

  • Hours of videos, demos & screen casts
  • 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.