I am creating a test file and I need to fill it with random times between 7AM to 11AM. Repeating entries are OK as long as they aren't all the same
I'm also only interested in HH:MM (no seconds)
I don't know where to start. I did Google before posting and I found an interesting search result
www.random.org/clock-times/
Only issue is that all times "randomly" generated are in sequential order. I can put it out of sequence once but I need to generate 100 to 10,000 entries.
I am hoping to create a WinForm C# app that will help me do this.
Calculate the number of minutes between your start and stop times then generate a random number between 0 and the maximum number of minutes:
Random random = new Random();
TimeSpan start = TimeSpan.FromHours(7);
TimeSpan end = TimeSpan.FromHours(11);
int maxMinutes = (int)((end - start).TotalMinutes);
for (int i = 0; i < 100; ++i) {
int minutes = random.Next(maxMinutes);
TimeSpan t = start.Add(TimeSpan.FromMinutes(minutes));
// Do something with t...
}
Notes:
Create a DateTime
value for the lower bound, and a random generator:
DateTime start = DateTime.Today.AddHours(7);
Random rnd = new Random();
Now you can create random times by adding minutes to it:
DateTime value = start.AddMinutes(rnd.Next(241));
To format it as HH:MM you can use a custom format:
string time = value.ToString("HH:mm");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With