views:

184

answers:

6

I have a set of events that must occur randomly, but in a predefined frequency. i.e over a course of (totally) infinite events, event A should have occured 10% of the times, event B should have occured 3%, and so on... Of course the total sum of the percentages of the event list will add upto 100.

I want to achieve this programmatically. How do I do this?

A: 

interesting description. Without specific details constricting impementation, I can only offer an idea that you can modify to fit into the choices you've already made about your implementation. If you have a file for which every line contains a single event, then construct the file to have 10% A lines, 3% B lines, etc. Then when choosing an event, get an integer randomly generated to select a line number from the file.

yetanotherdave
A: 

You have to elaborate a little more on what you mean. If you just want the probabilities to be as you described, just pick a random number between 1-100 and map it to the events. That is, if the random number is 1-10, do Event A. If it's 11-13, do Event B, etc.

However, if you require things to come out exactly with those proportions at all times (not that this is really possible), you have to do it differently. Please confirm which meaning you are looking for and I'll edit if needed.

Chad Birch
Yes, I am aware that the probabilities will fall into the required values only over a longer time, so I think that's sufficient for me. I guess it also depends on how "random" the pseudo random numbers generated are too.
+1  A: 

For each event, generate a random number between 0 and 100. If event A should occur 10% of the times, map values 0 - 10 to event A, and so on.

For instance, for 2 events:

n = 0 - 10 ==> Event A
n = 11 - 99 ==> Event B

If you do this, you can have your events occur at random times, and if the running time is long enough (and your RNG is good enough), event frequencies will add up to the desired percentage.

driis
0 - 10 will result in Event A occurring 11% of the time, this should be 0 - 9
Patrick McDonald
+4  A: 

You haven't specified a language, so here comes some pseudo-code

You basically want a function which will call other functions with various probabilities

Function RandomEvent

    float roll = Random() -- Random number between 0 and 1
    if roll < 0.1 then
        EventA
    else if roll < 0.13 then
        EventB
    ....
Patrick McDonald
A: 
  1. Generate a sequence of events in the exact proportions you want.
  2. For each event, randomly generate a timestamp when each event should be delivered, within your time bounds.
  3. Sort by that timestamp
  4. Run through the list, delivering each event at the appropriate time.
Michael
A: 

Choose a random number from 1 to 100 inclusive. Assign each event a unique set of integers that represents the frequency that it should occur. If you randomly generated number falls within that particular selected range of numbers fire the associated event.

In the example above the event that should show 10% of the time you would assign it a range of integers 10 integers long (1-10, 12-21, etc...). How you store these integer rangess is up to you.

Like Michael said, since these are random numbers there is no way to guarantee said event fires exactly 10% of the time but over the long run it should...given an even distribution of random numbers.

William Edmondson