无状态实时生成随机变量

发布于 2024-10-17 15:15:43 字数 315 浏览 6 评论 0原文

我想要一个函数,它以自上次调用以来经过的秒数作为输入,并返回 true 或 false 来判断事件是否应该在该时间段内发生。我希望它平均每经过 X 时间(比如 5 秒)就会触发一次。我也很感兴趣是否可以在没有任何状态的情况下进行操作,答案来自 this使用的问题

我想为了完全准确,它必须返回一个整数来表示应该发生的事件数量,在每 10*X 次或类似的情况下调用一次的情况下,所以奖励积分!

I want a function which takes, as input, the number of seconds elapsed since the last time it was called, and returns true or false for whether an event should have happened in that time period. I want it such that it will fire, on average, once per X time passed, say 5 seconds. I also am interested if it's possible to do without any state, which the answer from this question used.

I guess to be fully accurate it would have to return an integer for the number of events that should've happened, in the case of it being called once every 10*X times or something like that, so bonus points for that!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(2

嗫嚅 2024-10-24 15:15:43

听起来您正在描述一个 泊松过程,其中包含给定时间内的平均事件数间隔由参数 lambda=1/X 的 泊松分布 给出。

后一页的表达式的使用方法如下,对于给定的 lambda 值和 t 的参数值:

  1. 计算 0 到 1 之间的随机数;调用此 p
  2. 计算 Pr(k=0) (即 exp(-lambda*t) * (lambda*t)**0 / Factorial(0 ))
  3. 如果这个数字大于p,则模拟事件的数量为0。END
  4. 否则,计算Pr(k=1)并相加到 Pr(k=0)
  5. 如果这个数字大于p,那么答案是1。END
  6. ...等等。

请注意,是的,如果 t 与 1/lambda(即 X)相比较大,则在一段时间内可能会出现多个事件。如果与 1/lambda 相比 t 总是很小,那么您不太可能在该周期内获得多个事件,因此算法会大大简化(如果 p < exp(-lambda*t),则0,否则 1)。

注 2:不能保证每个时间间隔 X 至少会获得一个事件。只是它会平均到该值。

(上面的内容相当超出我的想象;仔细测试你的实现)

It sounds like you're describing a Poisson process, with the mean number of events in a given time interval is given by the Poisson distribution with parameter lambda=1/X.

The way to use the expression on the latter page is as follows, for a given value of lambda, and the parameter value of t:

  1. Calculate a random number between zero and one; call this p
  2. Calculate Pr(k=0) (ie, exp(-lambda*t) * (lambda*t)**0 / factorial(0))
  3. If this number is bigger than p, then the number of simulated events is 0. END
  4. Otherwise, calculate Pr(k=1) and add it to Pr(k=0).
  5. If this number is bigger than p, then the answer is 1. END
  6. ...and so on.

Note that, yes, this can end up with more than one event in a time period, if t is large compared with 1/lambda (ie X). If t is always going to be small compared to 1/lambda, then you are very unlikely to get more than one event in the period, and so the algorithm is simplified considerably (if p < exp(-lambda*t), then 0, else 1).

Note 2: there is no guarantee that you will get at least one event per interval X. It's just that it'll average out to that.

(the above is rather off the top of my head; test your implementation carefully)

春庭雪 2024-10-24 15:15:43

假设某些事件类型平均每 10 秒发生一次,并且您想要打印事件发生的时间戳的模拟列表。

一个好的方法是每 1 秒生成一个 [0,9] 范围内的随机整数。如果为 0 - 触发这一秒的事件。当然,您可以控制分辨率:您可以每 0.1 秒生成一个 [0,99] 范围内的随机整数,如果它为 0,则触发此 DeciSecond 事件。

假设事件之间不存在依赖关系,则无需保留状态。

要找出给定时间片内事件发生的次数 - 只需根据所需的分辨率生成足够的随机整数即可。

编辑

您应该使用高分辨率(一个事件的每个周期至少 20 个随机数)才能使模拟有效。

Asssume some event type happens on average once per 10 seconds, and you want to print a simulated list of timestamps on which the events happened.

A good method would be to generate a random integer in the range [0,9] each 1 second. If it is 0 - fire the event for this second. Of course you can control the resolution: You can generate a random integer in the range [0,99] each 0.1 second, and if it comes 0 - fire the event for this DeciSecond.

Assuming there is no dependency between events, there is no need to keep state.

To find out how many times the event happens in a given timeslice - just generate enough random integers - according to the required resolution.

Edit

You should use high resolution (at least 20 randoms per period of one event) for the simulation to be valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文