So, we have to somehow fairly determine the winner of the XIDU Philbook Y13 giveaway. Our solution was to come up with a very basic (and hastily written) javascript program to randomly select the winner.


It’s based around a simple interval routine that runs every 100 milliseconds.


All the code in the red circle will run every 100 milliseconds.

A Closer Look at The Loop


The first line creates a new variable called ‘winnerNumber’ and it assigns it a random number somewhere between 1 and the length of the list of contestants.

Then, the second line creates a new variable called ‘winner’ and sets it to an array called ‘userlist’, the index of which is set by the random number created in the first line of code.

Finally, the third line of code outputs the ‘winner’ variable to the body.

This happens every 100 milliseconds, which is 10 times per second, selecting a new winner every time.

A Single Pass Interval to Select The Final Winner


Then, we use a single pass interval that has two jobs.


Its first job is to clear the ‘cycle’ interval, which stops the winner selection process, effectively choosing a winner.

After that, it stops itself so the program can end nicely.


Notice this time, instead of setting a hard-coded interval time for this one, we gave it a variable as a time instead.

That random time is generated with this line of code, which fits in between the two intervals.


As you can see, the ‘randomTimer’ variable is a random number between 5000 and 10000. This sets a random winner selection time of 5 to 10 seconds.

We will be running this program tonight at 11PM Eastern Standard Time. Sometime after that, we will post a video of the winner reveal.

Leave a Reply

Your email address will not be published. Required fields are marked *