1

I'm needing to generate 5 random times between 08:20:00 and 08:29:59.

These times need to be added into the top text boxes from left to right.

GUI

I'm currently doing it with this code:

private void Button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    Random random = new Random();
    TimeSpan start = TimeSpan.FromHours(08.20);
    TimeSpan end = TimeSpan.FromHours(08.30);
    int maxMinutes = (int)((end - start).TotalMinutes);

    for (int i = 0; i < 5; ++i)
    {
        int minutes = random.Next(maxMinutes);
        TimeSpan t = start.Add(TimeSpan.FromMinutes(minutes));
        listBox1.Items.Add(t);
    }
}

And this is what it looks like.

Currently, it's not generating them between the time frame and it's also not including seconds which is critical. I also need a solution to add them into their slots.

1st generated needs to go into monday_In, 2nd needs to go into tuesday_In, 3rd needs to go to wednesday_In, 4th needs to go to thursday_In, 5th needs to go to friday_In

Any ideas on how to do this?

Oliver Ketley
  • 35
  • 1
  • 8
  • 5
    Hint that may well be enough to get you going: `TimeSpan.FromHours(08.20)` isn't doing what you think it is. 8.2 hours is 8 hours and 12 minutes. – Jon Skeet Nov 14 '19 at 16:26
  • Also "TimeSpan.FromMinutes" does not add Seconds. – Emanuele Nov 14 '19 at 16:29
  • Right, I've been working from this solution. https://stackoverflow.com/questions/13590465/random-time-generator-for-time-betweeen-7am-to-11am – Oliver Ketley Nov 14 '19 at 16:30
  • 3
    On another note, `Random random = new Random();` should be declared once and re-used, not every time you click the button. – Trevor Nov 14 '19 at 16:31

2 Answers2

2

Firstly, 8.20 hours is not 8 hours and 20 minutes. It is 8 hours and 12 minutes. Similarly 8.30 hours is 8 hours and 18 minutes.

So you should change the way you initialise the start and end times:

var start = new TimeSpan(8, 20, 0);
var end = new TimeSpan(8, 30, 0);

Secondly, if you want to get seconds precision in generating a random time, you need the difference in seconds, not minutes:

var secondsDifference = (int)(end.TotalSeconds - start.TotalSeconds);

To get a random time, you can simply do startTime + x seconds where x is a random number between 0 and secondsDifference:

for (int i = 0 ; i < 5 ; i++) {
    var randomTime = start + TimeSpan.FromSeconds(random.Next(secondsDifference));
    listBox1.Items.Add(randomTime);
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0
private void Button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    Random random = new Random();
    long start = TimeSpan.FromHours(08.20).Ticks;
    long end = TimeSpan.FromHours(08.30).Ticks;

    for (int i = 0; i < 5; ++i)
    {
        long ticks = random.NextLong(start, end);
        TimeSpan t = TimeSpan.FromTicks(ticks);
        listBox1.Items.Add(t);
    }
}

edit

forgot that it was a custom extension

public static long NextLong(this Random random, long minValue, long maxValue)
{
    long dif = maxValue - minValue;
    return (long)Math.Round((random.NextDouble() * dif) + minValue);
}
  • That's not working. 'Random' does not contain a definition for 'NextLong' and no accessible extension method 'NextLong' accepting a first argument of type 'Random' could be found. I'll keep trying. – Oliver Ketley Nov 14 '19 at 17:00