Getting Ready For The ColdFusion Post-Christmas Raffle

Posted December 27, 2006 at 4:57 PM

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

 Launch code in new window » Download code as text file »

  • <!--- 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"
  • />

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

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

his small ColdFusion raffling algorithm takes a text file


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »