Winners Of My 1,000th ColdFusion Post

Posted January 26, 2009 at 9:53 AM

Tags: ColdFusion

Last Friday, I posted my 1,000th ColdFusion blog post on Kinky Solutions. In the spirit of loving ColdFusion, I reflected upon the last 8 years of my ColdFusion life regarding what ColdFusion has meant to me and I asked others to share their stories as well. I had said that from those pro-ColdFusion comments, I would pick some winners this morning to receive Amazon.com gift certificates. I wanted to pick those winner randomly. Now, picking random numbers in ColdFusion is a rather straightforward task. But, I wanted to keep this task interesting (it's Monday morning after all). So, rather than just using ColdFusion's RandRange() method, I created a custom RandRange() method powered by Google's "I'm Feeling Lucky" search functionality:

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

  • <cffunction
  • name="GoogleRandRange"
  • access="public"
  • returntype="numeric"
  • output="false"
  • hint="I use Google's Im Feeling Lucky functionality to help pick out a random number.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="MinValue"
  • type="numeric"
  • required="true"
  • hint="I am the lower-bound inclusive value."
  • />
  •  
  • <cfargument
  • name="MaxValue"
  • type="numeric"
  • required="true"
  • hint="I am the upper-bound inclusive value."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!--- Create a random set of keywords. --->
  • <cfset LOCAL.Keywords = [
  • "sexy", "muscle", "voluptuous", "saucy", "naughty",
  • "protein", "hairy", "sweet", "squating", "kinky",
  • "amazon", "fetish", "plump", "smooth", "fitness",
  • "smile", "fresh", "pumped", "delicious", "happy"
  • ] />
  •  
  • <!---
  • Shuffle keyword array to get a new, random result from
  • Google's I'm Feelin Lucky search results.
  • --->
  • <cfset CreateObject( "java", "java.util.Collections" ).Shuffle(
  • LOCAL.Keywords
  • ) />
  •  
  • <!--- Build the search query with the first 3 keywords. --->
  • <cfset LOCAL.Criteria = (
  • LOCAL.Keywords[ 1 ] & " " &
  • LOCAL.Keywords[ 2 ] & " " &
  • LOCAL.Keywords[ 3 ]
  • ) />
  •  
  •  
  • <!---
  • Get the first result from Google search. Wrap this in a
  • Try/Catch incase it timesout and throws an error.
  • --->
  • <cftry>
  •  
  • <!--- Grab random result. --->
  • <cfhttp
  • method="get"
  • url="http://www.google.com/search"
  • useragent="RandomizerBot"
  • timeout="5"
  • result="LOCAL.HttpGet">
  •  
  • <!--- Referer from google. --->
  • <cfhttpparam
  • type="header"
  • name="referer"
  • value="http://www.google.com"
  • />
  •  
  • <!--- The search query. --->
  • <cfhttpparam
  • type="formfield"
  • name="q"
  • value="#LOCAL.Criteria#"
  • />
  •  
  • <!--- Im Feeling Lucky submit button. --->
  • <cfhttpparam
  • type="formfield"
  • name="btnI"
  • value="I'm Feeling Lucky"
  • />
  •  
  • </cfhttp>
  •  
  • <!--- Catch http connection errors. --->
  • <cfcatch>
  •  
  • <!---
  • There was an error connecting to this site.
  • Try another attempt.
  • --->
  • <cfreturn GoogleRandRange(
  • ARGUMENTS.MinValue,
  • ARGUMENTS.MaxValue
  • ) />
  •  
  • </cfcatch>
  • </cftry>
  •  
  •  
  • <!--- Check to see if we connected. --->
  • <cfif NOT FindNoCase( "200", LOCAL.HttpGet.StatusCode )>
  •  
  • <!---
  • The random result may have failed. Return a
  • new random result.
  • --->
  • <cfreturn GoogleRandRange(
  • ARGUMENTS.MinValue,
  • ARGUMENTS.MaxValue
  • ) />
  •  
  • </cfif>
  •  
  •  
  • <!---
  • ASSERT: At this point, we know that the CFHTTP get
  • was successfull.
  • --->
  •  
  •  
  • <!--- Remove any non-word characters. --->
  • <cfset LOCAL.Content = REReplace(
  • LOCAL.HttpGet.FileContent,
  • "[^\w]+",
  • "",
  • "all"
  • ) />
  •  
  • <!--- Grab a random character in the content. --->
  • <cfset LOCAL.Char = Mid(
  • LOCAL.Content,
  • RandRange( 1, Len( LOCAL.Content ) ),
  • 1
  • ) />
  •  
  •  
  • <!--- Get the difference betweent he min and max value --->
  • <cfset LOCAL.Range = (ARGUMENTS.MaxValue - ARGUMENTS.MinValue) />
  •  
  • <!---
  • To make sure we get a sufficiently large value from
  • which to pick (increasing the randomness), let's multiple
  • the selected ASCII value by the length of the content.
  • --->
  • <cfset LOCAL.HighAscii = (Asc( LOCAL.Char ) * Len( LOCAL.Content )) />
  •  
  • <!---
  • Return the lower bound value plus the MOD of the high
  • ascii to the range.
  • --->
  • <cfreturn (
  • ARGUMENTS.MinValue +
  • (LOCAL.HighAscii MOD LOCAL.Range)
  • ) />
  • </cffunction>

Now, obviously, this method is not completely random since the I'm Feeling Lucky site is picked from a finite set of keyword combinations. However, for our purposes (which is just to have fun), this should be sufficient.

Picking The Winners

Once I had the above function in place, I counted up the qualifying comments from my previous post and then called the GoogleRandRange() method four times. Here are the winners of my 1,000th ColdFusion post and a $25 Amazon.com gift certificate:

  1. Tom Chiverton
  2. David Stamm
  3. Yves
  4. Matt Gifford

Congratulations guys! Let's keep the ColdFusion party going!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 26, 2009 at 10:10 AM // reply »
28 Comments

Holy cow - what a delightful surprise on a Monday morning!

And congratulations on your 1001st post, which includes one of the wackiest random number generators I've ever seen. :)


Jan 26, 2009 at 10:10 AM // reply »
18 Comments

Awesome!

Thanks Ben. You've made my Monday afternoon complete!
And with a truly 'Nadel' way of solving the winners. :)

Thank you


Jan 26, 2009 at 10:14 AM // reply »
42 Comments

Thank you very much Ben !
I'll let you know what it ends up being...


Jan 26, 2009 at 10:47 AM // reply »
6 Comments

Awesome!

You've made my day!!

I'm mostly a "lurker" on the blogosphere.... I've enjoyed reading many of your posts... loved the Object Orientation posts.

ColdFusion rocks.

;-)

Peace dude!


Jan 26, 2009 at 11:00 AM // reply »
153 Comments

That is a truly twisted and inspired way to get a random number. I'm a little bummed that you couldn't work QoQ into it, but then I know you need to protect the image of your sanity with the community.

Gratz on your kilopost.


Jan 26, 2009 at 11:00 AM // reply »
7,572 Comments

Congratulations guys. Thanks for sharing your stories. And please, feel free post comments any time - I love a good conversation.


Jan 26, 2009 at 11:01 AM // reply »
7,572 Comments

@Rick,

I was actually a bit inspired by your comments the other day. I can't remember the post, but I remember it has a query of queries :)


Jan 26, 2009 at 11:04 AM // reply »
106 Comments

Fix! Fix! :)

Congrats to all the winners and thanks Ben for giving back yet again to the community.


Jan 26, 2009 at 11:15 AM // reply »
7,572 Comments

@Gareth,

Blame Google :)


Jan 26, 2009 at 2:36 PM // reply »
40 Comments

A couple notes here-

First, is it possible to search ANYTHING related to programming without your site coming up first? It always even comes ahead of livedocs (which I am glad about). Great job.

Second, are there more than 24 hours in your day? You pump out a ton of content for only a 24 hour day. Keep it up. You inspire us all.


Jan 27, 2009 at 2:37 AM // reply »
14 Comments

haha, GoogleRandRange(), love it :)


Jan 27, 2009 at 6:35 PM // reply »
3 Comments

This is all backwards... Ben, the community owes you a huge debt for your hard work and dedication. I've said it many times and I'll say it again, THANK YOU!

Next time I'm in New York, I'd like to take you out for a beer or three ;-)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »