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,314 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,314 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,314 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
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools