I'm trying to randomize the value for a simple DateTime
data field.
I wish to get a random date/time between two date/times (e.g. min date/time and max date/time).
So lets imagine I'm after a random date/time between
1/1/2000 10:00:00am
and 1/1/2000 5:00:00pm
.
Also, this code will be used in a for loop, with 100 items ... meaning all 100 items will have random date/times between the min/max date/time period.
Any ideas?
You could try using:
This will give you different times down to the minute. If you want 100 (or any thing more than 1)
DateTime
s then only create theRandom
object once. The MSDN page onRandom
explains in detail why creating severalRandom
objects in quick succession is a bad idea.Using a different
TimeSpan
constructor will give you different granularity. From the TimeSpan constructor MSDN: