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.




Reader Comments

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

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


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

What do you mean exactly? This sounds cool.


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

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 »
11,238 Comments

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 »
27 Comments

store as date/time


Apr 30, 2007 at 5:30 PM // reply »
11,238 Comments

I gotcha. Cool stuff.


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 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools