On Wed, 01 Oct 2008 10:27:41 -0700, shapper <md*****@gmail.comwrote:
Hello,
How can I get a random date in a certain date range? For example:
Get random date between 27-02-2007 and 30-09-2008?
static Random rand = new Random();
DateTime GetRandomDate(DateTime dtStart, DateTime dtEnd)
{
int cdayRange = (dtEnd - dtStart).Days;
return dtStart.AddDays(rand.NextDouble() * cdayRange);
}
You may have to tweak the above, depending on whether you want the range
to be inclusive or exclusive of the end value, and whether you need the
returned DateTime to be with or without some fractional day component
(i.e. having a time other than midnight).
But hopefully the above gives you the general idea.
Pete