For a random event generator I’m writing I need a simple algorithm to generate random ranges.
So, for example:
I may say I want 10 random intervals, between 1/1 and 1/7, with no overlap, in the states (1,2,3) where state 1 events add up to 1 day, state 2 events add up to 2 days and state 3 events add up to the rest.
Or in code:
struct Interval { public DateTime Date; public long Duration; public int State; } struct StateSummary { public int State; public long TotalSeconds; } public Interval[] GetRandomIntervals(DateTime start, DateTime end, StateSummary[] sums, int totalEvents) { // insert your cool algorithm here }
I’m working on this now, but in case someone beats me to a solution (or knows of an elegant pre-existing algorithm) I’m posting this on SO.
Here is my current implementation that seems to work ok and accounts for all time. This would be so much cleaner if I didn’t have to target .net 1.1