St · Statistics & Probability

How to Generate a Poisson Random Variable from a Uniform One

Updated 13 October 2026

At a glance

  1. 1. Find the probabilities

    For the chosen rate lambda, start with the Poisson probability at zero and use the recurrence to get later probabilities.

  2. 2. Build cumulative totals

    Add the probabilities in order until the cumulative total reaches the uniform value.

  3. 3. Return the count

    The first count whose cumulative probability is at least the uniform draw is the Poisson sample.

  4. 4. Check the rate

    A rate of zero always returns zero; use a numerically robust method for very large rates.

A single standard uniform random variable can be transformed into a Poisson random variable by dividing the interval from 0 to 1 into pieces whose lengths match the Poisson probabilities. This is the inverse-transform method. It gives a direct algorithm for one sample; to generate independent Poisson samples, use independent uniform draws.

The inverse-transform idea

Let UU be uniform on [0,1)[0,1), and let XX have a Poisson distribution with rate parameter λ≥0\lambda\geq 0. Its probability mass function is

P(X=k)=pk=e−λλkk!,k=0,1,2,…P(X=k)=p_k=e^{-\lambda}\frac{\lambda^k}{k!},\qquad k=0,1,2,\ldots

Define the cumulative distribution function by F(k)=P(X≤k)=∑j=0kpjF(k)=P(X\leq k)=\sum_{j=0}^{k}p_j. The inverse-transform rule is to return the smallest nonnegative integer kk for which F(k)≥UF(k)\geq U.

Equivalently, assign the interval from F(k−1)F(k-1) to F(k)F(k) to the outcome kk, with F(−1)=0F(-1)=0. The interval’s length is F(k)−F(k−1)=pkF(k)-F(k-1)=p_k. A uniform draw lands in that interval with probability pkp_k, exactly the probability required for a Poisson outcome. The precise assignment of interval endpoints does not affect the distribution because any individual endpoint has probability zero.

A step-by-step algorithm

You do not need factorials or a table of probabilities. Start with p0=e−λp_0=e^{-\lambda}, then use the recurrence

pk=pk−1λk,k≥1.p_k=p_{k-1}\frac{\lambda}{k},\qquad k\geq 1.

Maintain a running cumulative total. Stop as soon as that total is at least UU.

  1. Set k=0k=0, p=e−λp=e^{-\lambda}, and C=pC=p.
  2. While U>CU>C, increase kk by 1, update p←pλ/kp\leftarrow p\lambda/k, and add pp to CC.
  3. Return kk.

For λ=0\lambda=0, return 0 directly. In an ideal mathematical calculation, the cumulative probabilities tend to 1 as kk increases. A computer-generated uniform is normally in [0,1)[0,1), so a finite count is returned.

Worked example

Take λ=3\lambda=3 and suppose the uniform draw is U=0.8U=0.8. The probability at zero is p0=e−3≈0.049787p_0=e^{-3}\approx 0.049787. The recurrence gives the following probabilities and cumulative totals:

kkpkp_k (approximately)F(k)F(k) (approximately)
00.0497870.049787
10.1493610.199148
20.2240420.423190
30.2240420.647232
40.1680310.815263

The cumulative total is below 0.8 at k=3k=3 and above it at k=4k=4. Therefore the transformed value is X=4X=4. In general, values of UU between about 0.647232 and 0.815263 produce 4 for this rate.

Python implementation

The function below transforms a supplied uniform value. It uses only the Python standard library’s math module and is suitable for ordinary, moderate values of λ\lambda.

import math

def poisson_from_uniform(u, lam):
    if not 0.0 <= u < 1.0:
        raise ValueError('u must be in [0, 1)')
    if lam < 0.0:
        raise ValueError('lam must be nonnegative')
    if lam == 0.0:
        return 0

    k = 0
    p = math.exp(-lam)
    cumulative = p

    while u > cumulative:
        k += 1
        p *= lam / k
        cumulative += p

    return k

For example, poisson_from_uniform(0.8, 3.0) returns 4. To generate a sample in a program, pass in a uniform value from a random-number generator; repeated samples intended to be independent should use independent draws.

Why the algorithm is correct

For a given kk, the procedure returns kk when the uniform draw falls in the portion of the unit interval allocated to that count. That portion has length F(k)−F(k−1)=pkF(k)-F(k-1)=p_k. Since UU is uniform, the probability of landing there is its length, so P(X=k)=pkP(X=k)=p_k. This verifies the full distribution, not just its mean or variance.

Common mistakes and numerical limits

  • Stopping at the first probability above UU instead of the cumulative total. Compare UU with F(k)F(k), the sum through kk, not with the individual probability pkp_k.
  • Using the wrong recurrence index. When advancing from k−1k-1 to kk, multiply by λ/k\lambda/k.
  • Treating λ\lambda as an integer. The rate can be any nonnegative real number; only the generated outcome must be an integer.
  • Passing U=1U=1. For λ>0\lambda>0, the inverse CDF does not reach 1 at any finite count in exact arithmetic; for λ=0\lambda=0, it reaches 1 at k=0k=0. Standard pseudorandom generators ordinarily return values below 1; if your source can return 1, handle that endpoint separately.
  • Using this direct recurrence for very large λ\lambda. It can require many iterations, and computing e−λe^{-\lambda} may underflow to zero in floating-point arithmetic. For large rates, use a tested Poisson random-variate routine based on a method designed for that range rather than relying on this simple inverse-CDF implementation.

The inverse-transform construction itself applies to any discrete distribution: use its cumulative probabilities and return the first outcome whose cumulative total reaches the uniform draw.

References

  1. Non-Uniform Random Variate Generation — Luc Devroye
  2. Simulation — Sheldon M. Ross

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.