Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get both Random Date and Time C#

I'm trying to write a code the generate random date and time where all values of (yyyy-mm-dd hh:mm:ss) are changed

I used this code but it change only the date and fix the time

 DateTime RandomDay()
        {
            DateTime start = new DateTime(2013, 1, 1 , 1 , 1 ,1);
            Random gen = new Random();

            int range = (DateTime.Today - start).Days;
            return start.AddDays(gen.Next(range));
        }

I said ok if I write a code that generate a random date, and store it in a variable1, then write another code that generate random time, and store it in variable2, then pick the date only from variable1, and pick the time only from variable2, then combine variable1 and variable2 together , so I can generate random date and time therefore, I wrote this another method to generate the time

DateTime RandomTime()
        {
            DateTime start = new DateTime(2013, 1, 1, 1, 1, 1);
            Random gen = new Random();

            int range = (DateTime.Today - start).Hours;
            return start.AddHours(gen.Next(range));
        }

now the problem in the RandomTime method, it generate Random hours only, doesn't change the values of the minutes and seconds,

is there any suggestion to solve the code problem? is there anyway to generate random values for all (yyyy-mm-dd hh:mm:ss) ?

like image 765
amal50 Avatar asked Dec 02 '15 00:12

amal50


2 Answers

Here's a method that'll randomly generate every portion of the date and time.

It uses appropriate limits so the generated date is valid (years within 1-9999, months within 1-12, a call to DateTime.DaysInMonth so we don't end up with Feb 31, etc).

public IEnumerable<DateTime> GenerateRandomDates(int numberOfDates)
{
    var rnd = new Random(Guid.NewGuid().GetHashCode());

    for (int i = 0; i < numberOfDates; i++)
    {
        var year = rnd.Next(1, 10000);
        var month = rnd.Next(1, 13);
        var days = rnd.Next(1, DateTime.DaysInMonth(year, month) + 1);

        yield return new DateTime(year, month, days,
            rnd.Next(0, 24), rnd.Next(0, 60), rnd.Next(0, 60), rnd.Next(0, 1000));
    }
}

To generate 10,000 of them:

var randomDateTimes = GenerateRandomDates(10000);
like image 62
Grant Winney Avatar answered Oct 20 '22 14:10

Grant Winney


Here is the one line solution. 1 line for the return part ;)

private static Random _ran = new Random();

public static DateTime RandomDateTime()
{
    return
        DateTime.MinValue.Add(
            TimeSpan.FromTicks((long) (_ran.NextDouble()*DateTime.MaxValue.Ticks)));
}

Note 1: _ran.NextDouble() gives random value between 0 and 1. so the amount of ticks you add to minimum datetime would be between 0 and DateTime.MaxValue.Ticks. So the random date time would be between minimum and maximum date time.

Note 2: DateTime.Min.Ticks is equal to 0. so this process will never overflow even if you add maximum date time ticks.


Here is how to generate 10k random date times.

DateTime[] datetimes = new DateTime[10000];

for (int i = 0; i < datetimes.Length; i++)
{
    datetimes[i] = RandomDateTime();
}

You can also use this method if you want to define ranges.

public static DateTime RandomDateTime(DateTime min, DateTime max)
{
    return
        DateTime.MinValue.Add(
            TimeSpan.FromTicks(min.Ticks + (long) (_ran.NextDouble()*(max.Ticks - min.Ticks))));
}
like image 3
M.kazem Akhgary Avatar answered Oct 20 '22 16:10

M.kazem Akhgary