How to get both random date and time in C #

I am trying to write code to generate random dates and times when all values โ€‹โ€‹change (yyyy-mm-dd hh: mm: ss)

I used this code, but it only changes the date and fixes 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, well, if I write a code that generates a random date and save it in variable1, then write another code that generates a random time and store it in variable2, then select the date only from variable1 and select the time only from variable2 and then combine variable1 and variable2 together so that I can generate random date and time so I wrote this other method to generate 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 is in the RandomTime method, it only generates random hours, does not change the values โ€‹โ€‹of minutes and seconds,

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

+5
source share
2 answers

Here is a method that will randomly generate each part of the date and time.

It uses the appropriate limits, so the generated date is valid (years within 1-9999, months within 1-12, call DateTime.DaysInMonth , so we wonโ€™t end on February 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 create 10,000 of them:

 var randomDateTimes = GenerateRandomDates(10000); 
+6
source

Here is a one line solution. 1 line for the returned 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 a random value between 0 and 1. therefore, the number of ticks added to the minimum date-time will be between 0 and DateTime.MaxValue.Ticks . Thus, the random date time must be between the minimum and maximum dates.

Note 2: DateTime.Min.Ticks is 0. Therefore, this process will never overflow, even if you add maximum timestamps.


Here's how to generate random dates in 10k.

 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)))); } 
+2
source

Source: https://habr.com/ru/post/1237207/


All Articles