Results Of Our Summer Giveaway!

In Contest by Robert Stanica

Results Of Our Summer Giveaway!

(If you want to find out who won without reading anything, scroll down to the bottom!)

Hope everyone is having an AWESOME summer so far! As the resident “computer guy” at Diarying, I don’t get much writing done. Usually I break things and then quickly fix them (hopefully) before anyone notices but today I get a chance to reveal the process that we used to pick our first giveaway winner. This post is going to include code and lots of numbers so for the less technically inclined, feel free to scroll down to get to the winner straight away.

We had two ways of increasing your entries:

  1. Repost the giveaway image and tell us what you were looking forward to this summer: 2 entries
  2. Tag a friend in the comment section of the giveaway image: 1 entry per friend
  3. Like the giveaway image: 1 entry

The big requirement here was that in order to be eligible for the giveaway, you had to be following us by the giveaway’s end date. No follow meant any accrued entries were instantly invalidated.

In total there were 117 entries from 83 participants. That gives us an average of 1.4 entries per person. In the future we’ll be looking at more ways to gain entries and even tweaking the current values to make some of the bigger asks more appealing.

My main priority was coming up with a method of programmatically picking a winner that was as fair as possible. I wanted to simulate the act of putting a bunch of names in a hat and drawing one at random. The first step was to create list of every participant and the number of entries they had in a way that could be manipulated using JavaScript. I decided on using an array of key-value pairs, where each key was the Instagram user’s name and the value was their number of entries. It ended up looking like this:

var participants = [
{ bujo_flo : 1 },
{ ginestudies : 1 },
{ lazyegg_studies : 4},
];

Next I wrote a simple loop that iterated through the participants array and added the Instagram user’s name once for each entry to a new array. This new array represented the hat from which I’d eventually draw a name:

var hat = [];
for(var i=0; i<participants.length; i++){
for(var user in participants[i]){
for(var j=0;j<participants[i][user];j++){
hat.push(user);
}
}
}

Outputting the value of hat after the loop finished resulted in:

[ "bujo_flo", "ginestudies", "lazyegg_studies", "lazyegg_studies", "lazyegg_studies", "lazyegg_studies" ]

We can confirm it worked by checking to see that user lazyegg_studies had 4 entries and appears 4 times in the hat variable.

Almost there! Now I need to virtually shake the hat so that the accounts get shuffled around randomly. To do this I used an implementation of the Fisher-Yates (aka Knuth) Shuffle algorithm:

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

// While there are still users to shuffle
while (0 !== currentIndex) {

// Pick a random user
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current user.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
};

In short, the algorithm picks a user name from our hat, sets it aside, picks another random user from the remaining users, and repeats until there are no users left. I shuffled the “hat” two times (just to be sure) and ended up with the hat array looking like:

[ "lazyegg_studies", "lazyegg_studies", "bujo_flo", "lazyegg_studies", "lazyegg_studies", "ginestudies" ]

And now to pick a winner! I have an array of Instagram user names in a randomized order so now I need a random index to pick the winner. JavaScript’s Math.random() function is pseudo-random. While this is good in most cases, I wanted to do one better. Random.org is a website that generates truly random numbers based on atmospheric noise. I gave it a min value of 0 and a max value of 116 and clicked “Generate”. Totally superfluous to use atmospheric noise in picking a small giveaway winner but we’re doing this for SCIENCE! After looking up the user at the generated index we got a winner:

lazyegg_studies

Congratulations @lazyegg_studies you’re the winner of our first giveaway! We’ll be contacting you shortly with more details.

Latest posts by Robert Stanica (see all)