无状态实时生成随机变量
我想要一个函数,它以自上次调用以来经过的秒数作为输入,并返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在描述一个 泊松过程,其中包含给定时间内的平均事件数间隔由参数 lambda=1/X 的 泊松分布 给出。
后一页的表达式的使用方法如下,对于给定的 lambda 值和 t 的参数值:
exp(-lambda*t) * (lambda*t)**0 / Factorial(0 )
)请注意,是的,如果 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:
exp(-lambda*t) * (lambda*t)**0 / factorial(0)
)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)
假设某些事件类型平均每 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.