Generating a set of random events at a predefined frequency

Generating a set of random events at a predefined frequency

鹤舞 发布于 2021-11-27 字数 392 浏览 875 回复 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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


年少掌心 2022-06-07 6 楼

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.

绮筵 2022-06-07 5 楼
  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.
夜光 2022-06-07 4 楼

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.

他不在意 2022-06-07 3 楼

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.

乖乖公主 2022-06-07 2 楼

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.

自由如风 2022-06-07 1 楼

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
    else if roll < 0.13 then