Getting Ready For The ColdFusion Post-Christmas Raffle

Posted December 27, 2006 at 4:57 PM by Ben Nadel

Tags: ColdFusion

This Christmas, I tried to give away some little "Kinky Solutions" gifts to people. Some people accepted, others passed. As a result, I have a few surplus gifts lying around that I would like to raffle off. In preparation for this post-Christmas gift raffle, I wrote a small little raffling system in ColdFusion that will help me select the names of anyone who is interested.

This small ColdFusion raffling algorithm takes a text file (.txt) of entry names and basically selects random records from it. However, I know that randomization on a computer is merely generated and not truly random. To help make the raffle more fair, for each entry selection I both shuffle the entries and then select a random index. Ok ok, maybe this has more to with entertaining myself than it does with making it fair... but come on, how often do I get to use the Java Collections object? Sometimes you just have to invent reasons to "use" when nothing logically presents itself :D

  • <!--- Read in the entries data file. --->
  • <cffile
  • action="READ"
  • file="#ExpandPath( './entries.txt' )#"
  • variable="lstEntries"
  • />
  •  
  •  
  • <!---
  • Convert the entries to an array. This is expecting a
  • single entry per line of the text file.
  • --->
  • <cfset arrEntries = ListToArray(
  • lstEntries,
  • "#Chr( 13 )##Chr( 10 )#"
  • ) />
  •  
  •  
  • <!---
  • Get the number of random entries to pick for raffle.
  • This is number of lucky winners :)
  • --->
  • <cfset intPickCount = 3 />
  •  
  • <!---
  • Create an array for selecting raffle entries
  • (the winning picks).
  • --->
  • <cfset arrPicks = ArrayNew( 1 ) />
  •  
  •  
  • <!---
  • Create a collection class. We are going to use the
  • static method Shuffle() to help randomize the winning
  • pick selection.
  • --->
  • <cfset objCollection = CreateObject(
  • "java",
  • "java.util.Collections"
  • ) />
  •  
  •  
  • <!---
  • Loop over the entries to select. We are going to only loop
  • enough times to pick the number of selected entries. Of
  • coures, if our entry list is smaller than our max pick
  • count, then we will only loop enough times to select every
  • one on the list (everyone wins).
  • --->
  • <cfloop
  • index="intI"
  • from="1"
  • to="#Min( intPickCount, ArrayLen( arrEntries ) )#"
  • step="1">
  •  
  • <!---
  • Shuffle the entries array. Since the arrays are passed
  • by reference into the Shuffle() method (or perhaps more
  • appropriately, passed BACK by reference), we do not
  • need to store the resultant array back into itself.
  • --->
  • <cfset objCollection.Shuffle( arrEntries ) />
  •  
  • <!---
  • Even though the entries array has been shuffled, let's
  • also select a random index from within the entries to
  • help randomize.
  • --->
  • <cfset intEntry = RandRange( 1, ArrayLen( arrEntries ) ) />
  •  
  • <!--- Add that index to the picks. --->
  • <cfset ArrayAppend( arrPicks, arrEntries[ intEntry ] ) />
  •  
  • <!---
  • Delete selected pick so that it cannot be selected
  • again in a future loop iteration.
  • --->
  • <cfset ArrayDeleteAt( arrEntries, intEntry ) />
  •  
  • </cfloop>
  •  
  • <!---
  • Write the entries to file. In order to convert the array
  • back to a text file, we are going to convert it to a
  • list that uses line breaks and carriage returns as
  • its item delimiters.
  • --->
  • <cffile
  • action="WRITE"
  • file="#ExpandPath( './winners.txt' )#"
  • output="#ArrayToList( arrPicks, (Chr( 13 ) & Chr ( 10 )) )#"
  • addnewline="false"
  • fixnewline="true"
  • />


Reader Comments

Aug 3, 2009 at 10:34 AM // reply »
1 Comments

his small ColdFusion raffling algorithm takes a text file


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools