3

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?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

2 Answers2

4

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;
fairidox
  • 3,358
  • 2
  • 28
  • 29
  • ah nice, i didn't think of just saving the next time it would happen. i thought of a function that would just be called, only use its input, and have that property. actually that would be neat.. i'll ask for it in another question – Claudiu Feb 14 '11 at 05:06
  • For a justification of this approach and some code read here: http://stackoverflow.com/a/15307412/1650437 – jdbertron Mar 09 '13 at 05:12
0

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.

pjs
  • 18,696
  • 4
  • 27
  • 56