Skip to main content
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Zeljka Majetic
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Zeljka Majetic

Winners Of My 1,000th ColdFusion Post

By
Published in Comments (12)

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:

<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!

Want to use code from this post? Check out the license.

Reader Comments

29 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. :)

7 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!

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.

15,902 Comments

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

15,902 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 :)

42 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.

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 ;-)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel