Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:21:01+00:00 2026-05-10T19:21:01+00:00

For a random event generator I’m writing I need a simple algorithm to generate

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:21:02+00:00Added an answer on May 10, 2026 at 7:21 pm

    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

    public class Interval {     public Interval(int state)     {         this.State = state;         this.Duration = -1;          this.Date = DateTime.MinValue;     }     public DateTime Date;     public long Duration;      public int State;  }  class StateSummary {     public StateSummary(StateEnum state, long totalSeconds)     {            State = (int)state;         TotalSeconds = totalSeconds;     }     public int State;     public long TotalSeconds;  }  Interval[] GetRandomIntervals(DateTime start, DateTime end, StateSummary[] sums, int totalEvents) {     Random r = new Random();      ArrayList intervals = new ArrayList();      for (int i=0; i < sums.Length; i++)     {         intervals.Add(new Interval(sums[i].State));     }      for (int i=0; i < totalEvents - sums.Length; i++)     {         intervals.Add(new Interval(sums[r.Next(0,sums.Length)].State));     }      Hashtable eventCounts = new Hashtable();     foreach (Interval interval in intervals)     {         if (eventCounts[interval.State] == null)          {             eventCounts[interval.State] = 1;          }         else          {             eventCounts[interval.State] = ((int)eventCounts[interval.State]) + 1;         }     }      foreach(StateSummary sum in sums)     {         long avgDuration = sum.TotalSeconds / (int)eventCounts[sum.State];         foreach (Interval interval in intervals)          {             if (interval.State == sum.State)             {                 long offset = ((long)(r.NextDouble() * avgDuration)) - (avgDuration / 2);                  interval.Duration = avgDuration + offset;              }         }     }       // cap the durations.      Hashtable eventTotals = new Hashtable();     foreach (Interval interval in intervals)     {         if (eventTotals[interval.State] == null)          {             eventTotals[interval.State] = interval.Duration;          }         else          {             eventTotals[interval.State] = ((long)eventTotals[interval.State]) + interval.Duration;         }     }      foreach(StateSummary sum in sums)     {         long diff = sum.TotalSeconds - (long)eventTotals[sum.State];         if (diff != 0)         {             long diffPerInterval = diff / (int)eventCounts[sum.State];              long mod = diff % (int)eventCounts[sum.State];             bool first = true;             foreach (Interval interval in intervals)              {                 if (interval.State == sum.State)                 {                     interval.Duration += diffPerInterval;                     if (first)                      {                         interval.Duration += mod;                         first = false;                     }                  }             }         }     }      Shuffle(intervals);      DateTime d = start;      foreach (Interval interval in intervals)      {         interval.Date = d;          d = d.AddSeconds(interval.Duration);     }      return (Interval[])intervals.ToArray(typeof(Interval)); }  public static ICollection Shuffle(ICollection c) {     Random rng = new Random();     object[] a = new object[c.Count];     c.CopyTo(a, 0);     byte[] b = new byte[a.Length];     rng.NextBytes(b);     Array.Sort(b, a);     return new ArrayList(a); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, you could call the method that does exactly what… May 12, 2026 at 6:00 am
  • Editorial Team
    Editorial Team added an answer Example: SELECT [EmployeeKey] ,[ParentEmployeeKey] ,[FirstName] ,[LastName] ,[MiddleName] ,[DepartmentName] AS "comment()"… May 12, 2026 at 6:00 am
  • Editorial Team
    Editorial Team added an answer A simple way of doing this is to use Password… May 12, 2026 at 6:00 am

Related Questions

For a random event generator I'm writing I need a simple algorithm to generate
I need a simple random English sentence generator. I need to populate it with
I have a small project I am doing in Python using web.py. It's a
You usually normalize a database to avoid data redundancy. It's easy to see in
How are random numbers generated.? How do languages such as java etc generate random

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.