1. Home
  2. Statistics & special functions

Statistics · random variates

Binomial Random Numbers (binornd)

Eigenfield editorial team ·

A function such as binornd returns random counts drawn from a binomial distribution: the number of successes in n independent trials, each succeeding with probability p. Called with a size argument it fills a vector or matrix with such counts. The inputs are n, p and the shape of the output; the only raw material is a stream of uniform random numbers on the interval (0, 1).

Each draw has mean np and variance np(1 − p). For n = 5 and p = 0.3 those are 1.5 and 1.05, which gives a quick check on any batch of output: the sample mean of N draws should land within a few multiples of √(1.05/N) of 1.5. With N = 10,000 that standard error is about 0.010.

Method one: count the successes

The definition is itself an algorithm. Draw n uniform numbers and count how many fall below p. Take n = 5 and p = 0.3 with the uniforms 0.12, 0.64, 0.25, 0.91 and 0.08. Three of them, 0.12, 0.25 and 0.08, are below 0.3, so the draw is 3.

The method is exact and trivial to write, but the cost grows linearly with n. For a thousand trials per draw and a million draws, that means a billion uniform numbers.

Method two: table inversion

Inversion uses one uniform number per draw regardless of n. Build the cumulative distribution once, then for each draw find the smallest k whose cumulative probability reaches u.

The probabilities come from a recurrence that avoids factorials: P(0) = (1 − p)ⁿ and P(k + 1) = P(k) · (n − k)/(k + 1) · p/(1 − p). For n = 5 and p = 0.3 the ratio p/(1 − p) is 3/7, and the steps run as follows.

k P(k) cumulative
0 0.16807 0.16807
1 0.36015 0.52822
2 0.30870 0.83692
3 0.13230 0.96922
4 0.02835 0.99757
5 0.00243 1.00000

Now draw. A uniform number u = 0.60 lies above 0.52822 and at or below 0.83692, so k = 2. The value u = 0.90 gives k = 3, and u = 0.05 gives k = 0. A search through the cumulative column can be linear, starting from the most probable value, or binary when n is large.

The first entry deserves attention. For large n and moderate p, (1 − p)ⁿ underflows, and the whole recurrence collapses to zero. Start from the mode instead, or work with logarithms of the probabilities.

Practical choices

For p above one half, draw with success probability 1 − p and return n minus the result. That keeps the recurrence short because the table has less mass in the far tail.

When n is large, say in the hundreds or more, inversion tables get long and rejection methods that cost a constant number of uniform numbers per draw take over. Their details are intricate, and a correct implementation is validated by the same moment checks as the simple ones. Approximating the draw by a rounded normal variate is not exact; it distorts the tails and sometimes returns values outside 0 … n.

The edge cases are cheap to handle explicitly. If p = 0 return 0, if p = 1 return n, and reject n that is not a non-negative integer.

Validating a generator

Three tests are enough for most purposes. Compare the sample mean and variance with np and np(1 − p). Compare the observed frequencies with the table above by building a histogram of many draws. Finally, check that every output is an integer between 0 and n.

Related generators produce Poisson random numbers and gamma random numbers. A binomial probability with large n and small p is well approximated by the Poisson distribution, which is why the two pages share their testing habits. More distribution tools are indexed under statistics and special functions.

Back to top