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
2010-04-21 14:35:51
+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
2010-04-21 14:39:28
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
2010-04-21 14:40:36
+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
2010-04-21 14:42:17