CFML Mythbusters competition: Choosing the winners

Posted by Mark Drew on code on February 2, 2012

Tagged under book,railo

A couple of weeks ago, I posted about running a competition to be in my CFML Mythbusters competition, well the competition is now over and it's time to choose the winners

Before choosing them, I thought I would post the code I am using to choose them and the process, so to be nice and transparent.

First off, everyone could post multiple times and this went into a Google Docs spreadsheet, so they could have multiple entries. Since I could see how many entries there were, for example 1-20, this is easy.

I decided to create an array (because they are much sexier than lists), each position with the number of the entry. Then randomise those positions and then simply choose positions 1,2,3 and 4 from their positions in their array

So here is the code (not including the form, since I am pretty sure you know how to make a form.):

aEntries = []; loop from="#FORM.from#" to="#FORM.to#" index="e"\{ ArrayAppend(aEntries,e); }

		collection = CreateObject("java", "java.util.Collections");
		collection.shuffle(aEntries);
	</cfscript>
	<cfoutput>
	<ol>
		<li>First Place: #aEntries[1]#</li>
		<li>Second Places: #aEntries[2]# and #aEntries[3]#</li>
		<li>Third Place: #aEntries[4]#</li>
	</ol>
	</cfoutput> </code>

Did you spot the Java class? Yep! The java.util.Collections class is really handy for manipulating Arrays in different ways, and of course, since (in Railo at least) an Array can be used by the Collections it is perfect.

So what do you think? Is this a fair way to select some random people?




comments powered by Disqus