tags:

views:

293

answers:

5

Hi folks,

I'm trying to randomize the value for a simple DateTime datafield.

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 10am and 1/1/2000 5pm.

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.

phew!

any ideas?

+3  A: 

You could try using:

TimeSpan timeSpan = endDate - startDate;
var randomTest = new Random();
TimeSpan newSpan = new TimeSpan(0, randomTest.Next(0, (int)timeSpan.TotalMinutes), 0);
DateTime newDate = startDate + newSpan;

This will give you different times down to the minute. If you want 100 (or any thing more than 1) DateTimes then only create the Random object once. The MSDN page on Random explains in detail why creating several Random objects in quick succession is a bad idea.

Using a different TimeSpan constructor will give you different granularity. From the TimeSpan constructor MSDN:

TimeSpan(Int64) Initializes a new TimeSpan to the specified number of ticks.
TimeSpan(Int32, Int32, Int32) Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.
TimeSpan(Int32, Int32, Int32, Int32) Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.
TimeSpan(Int32, Int32, Int32, Int32, Int32) Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.

ChrisF
+2  A: 

Really quickly:

  1. convert your dates to TotalHours
  2. Subtract one number from the other and use Abs to make positive
  3. Create a random number within the range of 1 and the result of 2. above
  4. Add the resulting random number of hours back to the earlier of your two dates
RedFilter
I was just giving the same answer when yours popped up ;)
slashmais
Was trying to attach this comment to the last post...remember that Random.Next in .NET is non-inclusive to the upper bound.
NickLarsen
+1  A: 

First, figure out what the precision is that you want on random DateTime (hours,minutes,seconds,ms,etc).

Then figure out the difference between the two dates in that unit.

Create a random integer between 0 and that difference.

Add the random integer in units to the original date.

Given the use case you stated above, calculate the difference outside in the for loop.

Inside the for loop, get the random int and construct the random date.

tschaible
A: 

Here's my algorithm and code:

  • find the difference between the two dates
  • for each iteration, create a random number between the two dates
  • create a new date between them. Simply add that random number as minutes to the start datetime.  

    Random randNum = new Random();
    
    
    DateTime minDt = new DateTime(2000,1,1,10,0,0);
    DateTime maxDt = new DateTime(2000,1,1,17,0,0);
    List<DateTime> myDates = new List<DateTime>();
    //Random.Next in .NET is non-inclusive to the upper bound (@NickLarsen)
    int minutesDiff = Convert.ToInt32(maxDt.Subtract(minDt).TotalMinutes+1);
    
    
    for (int i = 0; i < 100; i++)
    {
       // some random number that's no larger than minutesDiff, no smaller than 1
       int r=   randNum.Next(1, minutesDiff); 
       myDates.Add(minDt.AddMinutes(r));
    }
    
    
    foreach (DateTime d in myDates)
    {
      Console.WriteLine(string.Format("{0:dd-MMM-yyyy hh:mm}",d));
    }
    
p.campbell
A: 

Here's a method using a random number of ticks:

Random r= new Random(); 
    //for better randomness don't recreate a new Random() too frequently.
long rand62bit = (((long)r.Next())<<31) + r.Next(); 
    // 62bits suffices for random datetimes, 31 does not!
DateTime newDate = startDate + new TimeSpan(rand62bit % (endDate - startDate).Ticks);

This method is exclusive the last date and inclusive the first. You can easily include the last date by adding one tick to the basic (endDate - startDate).Ticks quantity.

Eamon Nerbonne