Using Rand() To Generate Random Times In ColdFusion

Posted April 30, 2007 at 1:13 PM by Ben Nadel

Tags: ColdFusion

I was on the way to the bathroom (the Tea has to go somewhere) when this popped into my head: Since dates in ColdFusion are represented as floating numbers and the decimal represents the time of day, you can use ColdFusion's Rand() function to generate random times. Think about it; since Rand() gets a value that is between 0 and 1, this can be thought of as between 12:00 AM one day and 12:00 AM the next (1 = 12:00 AM of the next day).

Here's a little test:

• <cfloop
• index="intI"
• from="1"
• to="10"
• step="1">
•
• #TimeFormat( Rand(), "hh:mm TT" )#
•
• </cfloop>

Running the above code, we get:

09:46 PM
07:10 AM
07:48 AM
07:54 AM
03:20 PM
03:44 AM
08:23 PM
04:07 AM
10:05 PM
01:46 PM

Well anyway, this is really not that useful, but you never know. You might come up against a situation where picking random times is critical?? Good to know that this can easily be done.

Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

Apr 30, 2007 at 2:55 PM // reply »

We actually use this format for searching. It helps us a great deal.

Apr 30, 2007 at 3:00 PM // reply »

What do you mean exactly? This sounds cool.

Apr 30, 2007 at 3:27 PM // reply »

Here's an example:
"Service on between" and then you specify 2 dates
"Service on between" 1/12/2003 and 04/01/2007.

First of all you need to compare each value to the values stored in the db. Then you need to compare that 1/12/2003 is in between the dates stored in the db; same thing for 04/01/2007; and then you need to do the same for all the dates between 1/12/2003 and 04/01/2007. If you do date comparison, it will take a long time to get results. I'm not even talking about if you have a lot of results and the date ranges are bigger. Now, if you convert the dates to decimal point numbers, you will be comparing the numbers and not the dates, which is much faster and more efficient.

Apr 30, 2007 at 4:20 PM // reply »

Ahhh, I see what you are saying. But, do you mean you actually store the value in a DateTime field in the database? Or is a float/decimal field type?

Apr 30, 2007 at 4:25 PM // reply »

store as date/time

Apr 30, 2007 at 5:30 PM // reply »

I gotcha. Cool stuff.

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.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 8, 2014 at 5:21 PM
hi Ben, how can we mention our own custom tags instead of using HTML tags in coldfusion? kindly revert with an example. ... read »
Mar 8, 2014 at 1:28 PM
Experimenting With Multiple Class Inheritance In Javascript
Hi, Agree with Artavazd, it's not inheritance. The proof : console.log(ben instanceof Person) // false console.log(ben instanceof Monkey) // false console.log(ben instanceof Ben) // true If " ... read »
Mar 7, 2014 at 8:31 PM
Sanity Check: \$index vs. DOM In AngularJS Directives
I had NOOOO idea you could pass the entire friend object into a method like this: removeFriend( friend ) . I was always using some ID of the object and passing that around back and forth between vie ... read »
Mar 7, 2014 at 10:43 AM
Project HUGE: Active Release Technique (ART) With Dr. Christopher Anselmi In NYC
Does anyone know of a GOOD A.R.T. Provider near the Binghamton,NY area? I have gone to a couple of people that said they do ART but they don't. It's just a massage. Thanks for any help you can give ... read »
Mar 7, 2014 at 8:44 AM
GMail Seems To Ignore The Return-Path Header Defined By The CFMail FailTo Attribute
So, is the header "Problems-to" no longer valid? I also add "return-path" via cfmailparam. ... read »
Mar 6, 2014 at 8:58 PM
Posting XML With ColdFusion, CFHttp, And CFHttpParam
ERROR2: Missing type node :( ... read »
Mar 6, 2014 at 6:56 AM
CFLoop Attributes Evaluated Only Once
Hi Ben, An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF woul ... read »
Mar 5, 2014 at 6:27 AM
Defining Instantiatable Classes In The AngularJS Dependency Injection Framework