The Code that Picked the Contest Winner

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.

srandom(time(NULL));

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 forApp 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 {

srandom(time(NULL));

int positionOfLuckyWinnerInMyMailingList = random() % 112 + 1;

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

[ASFunctions alert: winnerIs];

[window makeKeyAndVisible];

}

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

}

@end

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

Picture 1.jpg

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!

One Response to The Code that Picked the Contest Winner

  1. The Code that Picked the Conte May 18, 2009 at 1:43 pm #

    [...] the Contest Winner May 18, 2009, 1:42 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/05/the-code-that-picked-the-contest-winner/) As some of you may know, as part of the promotion for my new eBook, How to Make an iPhone App, I [...]

Leave a Reply

Switch to our mobile site