Home > Blockchain >  How to generate random datetime in specific format ? and how to add some duplicated datetime?
How to generate random datetime in specific format ? and how to add some duplicated datetime?

Time:12-28

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Random_Files
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var randomDateTimes = GenerateRandomDates(10000);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        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));
            }
        }
    }
}

this generate 10,000 random datetime list. but i want each datetime to be in this format : ToString("yyyyMMddHHmm")

and i want that some datetime will be duplicated i mean the same for example 10,000 and in this 10,000 some of them will be the same.

for example : 31/10/2099 05:51:36 to be twice or more times in the random list. more to be random once but some of them to be the same. those are the same also to be random.

for example index 31 and index 77 the same or index 0 and index 791 the same. because later when i make out of this files names i want to compare the files names so i need some names to be the same.

CodePudding user response:

Note that DateTime doesn't have its own format; DateTime is just a struct. I suggest generating random DateTime within minDate (included) and maxDate (excluded):

private static DateTime RandomDateTime(DateTime minDate, 
                                       DateTime maxDate, 
                                       Random random = default) {
  if (minDate >= maxDate)
    throw new ArgumentOutOfRangeException(nameof(maxDate));

   random ??= Random.Shared;

   return minDate.AddTicks((long) (random.NextDouble() * 
                                   (maxDate.Ticks - minDate.Ticks)));
}

and then generate regured dates which turn them into strings using required format:

Random random = new Random(123);

string report = string.Join(Environment.NewLine, Enumerable
  .Range(1, 10)
  .Select(_ => RandomDateTime(new DateTime(2022, 1, 1), 
                              new DateTime(2023, 1, 1), 
                              random))
  .Select(date => $"{date:yyyyMMddHHmm}"));

Console.Write(report);

Output (fiddle):

202212260843
202211280827
202209290927
202210240558
202209271542
202201181514
202201070455
202202241223
202203130136
202208182335

CodePudding user response:

I like Dmitry's answer, but the only change I'd make would be to make a DateTime generator like this:

IEnumerable<DateTime> GenerateRandomDateTimes(DateTime minimum, DateTime maximum, Random random = default)
{
    if (minimum >= maximum)
        throw new ArgumentOutOfRangeException(nameof(maximum));

    random ??= Random.Shared;
    
    while (true)
    {
        yield return minimum.AddTicks(random.NextInt64(maximum.Ticks - minimum.Ticks));
    }
}

I then start with these paramters:

int requiredTotalCount = 10;
int requiredDuplicates = 2;

DateTime minimum = new DateTime(2022, 1, 1);
DateTime maximum = new DateTime(2023, 1, 1);

It's easy to generate the dates:

DateTime[] results =
    GenerateRandomDateTimes(minimum, maximum)
        .Take(requiredTotalCount - requiredDuplicates)
        .ToArray();
        
results =
    results
        .Concat(results.Take(requiredDuplicates))
        .ToArray();

Then, finally, I might want the order of the results randomized:

results = results.OrderBy(_ => Random.Shared.Next()).ToArray();
  • Related