I need to generate custom random time in c# - not to random for a few minutes but only for 15 minutes intervals, for example - not to get 17:11 but 17:15. How can I generate this? I'm new to this issue. Thank you
-
Hey, can you try to copy the code that you already have please? Cause as I understand you just want to round a giving time to the closest quart hour – Dylan El Bar Apr 26 '22 at 13:58
-
2https://stackoverflow.com/questions/7029353/how-can-i-round-up-the-time-to-the-nearest-x-minutes?answertab=scoredesc#tab-top – Toivo Säwén Apr 26 '22 at 13:59
-
It's a little ambiguous what you want. Please be more specific. – Reza Heidari Apr 26 '22 at 13:59
-
1Any time you want to do something random, you should create a `Random` object, call the appropriate method and then process the result however is appropriate. In your case, generate a random `int` in the appropriate range, multiply the result by 15 and add that many minutes to your base time. This is just logic, so you don't need any C# experience to come up with that. – user18387401 Apr 26 '22 at 14:00
-
Does this answer your question? [How can I round up the time to the nearest X minutes?](https://stackoverflow.com/questions/7029353/how-can-i-round-up-the-time-to-the-nearest-x-minutes) – Toivo Säwén Apr 26 '22 at 14:00
-
1@Toivo This old question has only half the answer. The random part is not answered there. – Dialecticus Apr 26 '22 at 14:03
-
I have no code still, I have no idea how to write it... More clear explanation: I need to random any hour, but not to random times like 17:11. I dont want to random only in template of 15 minutes. – hello Apr 26 '22 at 14:04
3 Answers
There are 24 hours in a day, and 4 slices of 15 minutes in every hour. This means you need a random integer in range [0, 24*4)
(0 inclusive, upper limit exclusive). With this integer value you get the time pretty easy. The time is N / 4
hours and (N % 4) * 15
minutes, But TimeSpan
already has a method to do most of the calculations for you. The code is something like this:
// make sure you have a single shared random, and not create it every time like this
Random rnd = new Random();
int n = rnd.Next(24*4);
TimeSpan ts = TimeSpan.FromMinutes(n * 15);

- 16,400
- 7
- 43
- 103
-
-
Oh thank you so much Dialecticus and JonasH! This is exactly what I want. – hello Apr 26 '22 at 14:26
You didn't specify the range that the random datetime can be in so I will leave that open for you to add:
// Define the range with these two datetime values (currently 2 years from this moment):
DateTime min = DateTime.Now; // start
DateTime max = DateTime.Now.AddYears(2); // end
// Range in seconds
int range = (int)max.Subtract(min).TotalMinutes;
// generate random amount of minutes
int r = new Random().Next(0, range);
// add minutes to start date
DateTime rDate = min.AddMinutes(r);
// Set a timespan to round up to (you wanted 15 minutes)
TimeSpan span = TimeSpan.FromMinutes(15);
// Finally, we round it up and have the resulting random datetime!
DateTime result = new((rDate.Ticks + span.Ticks - 1) / span.Ticks * span.Ticks, rDate.Kind);
Now you only need to set your start and end date (so basically the range) with the min
and max
datetimes.

- 1,151
- 1
- 11
- 26
If you could define your requirements a little better, we could write a better answer.
Generally, use Random
to create a random year, month, date, hour, minute. Then I would use Torivo's link to round as appropriate.
If this doesn't exactly meet the requirements, please communicate a little more about the purpose and what you've tried.

- 353
- 2
- 11
-
Thank you. I will explain: I'm using a geneteic algorithm in order to build schedule for lessons that takes 15 minutes every one. In the algorithm - in the Generate() method I need to random values. One of the values is the hour of the lesson. – hello Apr 26 '22 at 14:12