Get Random Date From A Date Range

This is a quick trick to generate a random date between two date ranges.

First, create a function that generates a random number between two given values:


private static int RandomNumBetween(int smallNum, int bigNum)
{
Random rnd = new Random();
int randomValue = rnd.Next(smallNum, bigNum + 1);
return randomValue;
}

Now, use this function to generate any kind of date ranges like this:


var DOB = new DateTime(RandomNumBetween(1970, 1999), RandomNumBetween(1,12), RandomNumBetween(1,30));

 

Tags: