Using Rand() To Generate Random Times In ColdFusion

Posted April 30, 2007 at 1:13 PM

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:

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

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

Download Code Snippet ZIP File

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




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

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 »
6,516 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 »
6,516 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 »
6,516 Comments

I gotcha. Cool stuff.


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 »