views:

197

answers:

5

The title pretty much says it all. Don't overlook the 'date AND TIME' part though.

+2  A: 

I don't know about ruby but why don't you just generate an integer and use it together with a timestamp? Then simply convert the timestamp to your desired format.

theseion
A: 

Calculate the difference in for example minutes between the two dates, generate a random number between 0 and that number, and add that number of minutes to the first date.

+2  A: 

Use time.to_i() (see class Time) to convert your dates to integer, randomize between those two values, then reconvert to time and date with Time.at().

klez
A: 

Likewise, I don't know ruby, but all you need to do is represent your start and end dates as integers (such as milliseconds since the epoch); subtract the start from the end (call this delta) and then generate your random number between 0 and delta. The randomly generated date is then the start + delta. In pseudocode:

let start = ...
let end = ...
let delta = end - start
let rand = random in range [0, delta]
let randDate = start + rand
Matt Ball
+2  A: 
Time.at((date2.to_f - date1.to_f)*rand + date1.to_f)

You'll get a time object that is between two given datetimes.

Evgeny Shadchnev