mercredi 28 décembre 2022

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

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.




Aucun commentaire:

Enregistrer un commentaire