Generate (Poisson?) random variable in real-time

2019-02-15 09:56发布

I have a program running in real-time, with variable framerate, e.g. can be 15 fps, can be 60fps. I want an event to happen, on average, once every 5 seconds. Each frame, I want to call a function which takes the time since last frame as input, and returns True on average once every 5 seconds of elapsed-time given it's called. I figure something to do with Poisson distribution.. how would I do this?

2条回答
叛逆
2楼-- · 2019-02-15 10:39

A random variable which generates true/false outcomes in fixed proportions with independent trials is called a Geometric random variable. In any time frame, generate true with probability 1/(5*fps) and in the long run you will get an average of one true per 5 seconds.

查看更多
叛逆
3楼-- · 2019-02-15 10:49

It really depends what distribution you want to use, all you specified was the mean. I would, like you said, expect that a Poisson distribution would suit your needs nicely but you also put "uniform random variable" in the title which is a different distribution, anyway let's just go with the former.

So if a Poisson distribution is what you want, you can generate samples pretty easily using the cumulative density function. Just follow the pseudocode here: Generating Poisson RVs, with 5 seconds being your value for lambda. Let's call this function Poisson_RN().

The algorithm at this point is pretty simple.

global float next_time = current_time()

boolean function foo()
if (next_time < current_time())
  next_time = current_time() + Poisson_RN();
  return true;
return false;
查看更多
登录 后发表回答