Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I am writing a simulator in which I am generating a flow of particle. I want this flow to follow a Poisson process, i.e. $\lambda$ particles are generated per second on average. So how to compute the time of the next particle generation? i.e. how to write the POI function below?

t = POI(lambda)
while true:
    if time_now() >= t:
        generate_a_particle()
        t = t + POI(lambda)

According to this post (http://math.stackexchange.com/questions/18894/exponential-distribution-from-poisson), I think I should draw a random number $u$ from the $[0,\lambda]$ interval, and compute t so that $f_X(t)=u$, which gives $$ t=\frac{Ln(\lambda/u)}{\lambda} $$

That would make POI be:

POI(lambda):
    u = random(0,lambda)
    return ln(lambda/u)/lambda

Is that correct?

share|improve this question
add comment

1 Answer

The time $T$ to the next event in a Poisson process of rate $\lambda$ has an exponential distribution with rate $\lambda$, so that $P(T \le t) = 1 - e^{-\lambda t}$ for $t \ge 0$. By taking $T = \ln(\lambda/U)/\lambda$ where $U$ is uniform in $(0,\lambda)$, you get $P(T \le t) = P(\lambda/U \le e^{\lambda t}) = P(U/\lambda \ge e^{-\lambda t}) = 1 - e^{-\lambda t}$. So yes, this is correct.

It might be slightly simpler to take a uniform random variable $V$ in $(0,1)$ (corresponding to $U/\lambda$), and thus $T = -\ln(V)/\lambda$.

You should be aware that, depending on the pseudo-random number generator being used, the value returned for a random number in $(0,1)$ may be $0$ (and e.g. a 16-bit generator might return this once in every 65536 calls), which could cause a problem when you try to take the logarithm or the reciprocal of that. To avoid that, you might add some small positive number to your $U$ or $V$, or perhaps just discard a result of $0$ and try again.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.