Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 57 ยท Common Distributions

The Uniform Distribution

Learn the discrete and continuous uniform distributions: equally likely outcomes, density, mean, variance and probabilities as areas, with Python examples you can run.

Some random processes have no favorites: a fair die lands on each face equally often, and a random number generator is meant to return any value in a range with equal chance. These are uniform distributions. They are the simplest models in probability, and also the raw material of simulation: almost every other random variable can be produced by transforming uniform random numbers.

Discrete uniform

A discrete uniform variable takes n equally likely values. For a fair die, X takes values 1 to 6, each with probability 1/6. For integers from a to b there are n = b - a + 1 values, so P(X = k) = 1/n.

TEXT
E[X]   = (a + b) / 2
Var(X) = (n^2 - 1) / 12

For a die: mean 3.5 and variance (36 - 1) / 12 = 2.9167.

Python A fair die

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Continuous uniform

A continuous uniform variable on the interval [a, b] can take any real value in that range, and equal-length sub-intervals have equal probability. Because there are infinitely many values, the probability of any single exact value is 0. Instead we use a density f(x), and probabilities are areas under it. Here the density is a flat rectangle:

TEXT
f(x) = 1 / (b - a)   for a <= x <= b,  and 0 otherwise

The height 1 / (b - a) is chosen so the rectangle's area, width b - a times height, equals 1.

TEXT
P(c <= X <= d) = (d - c) / (b - a)
E[X]   = (a + b) / 2
Var(X) = (b - a)^2 / 12

Notice what the probability formula says: the chance depends only on the length of the sub-interval, not where it sits.

Python Waiting for a bus

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A density is not a probability. For a continuous variable f(x) can be bigger than 1: on [0, 0.5] the uniform density is 2. Only the area f(x) * width is a probability.

Uniform random numbers as a building block

Computers generate numbers U ~ U(0, 1). To get a uniform value on [a, b], compute a + (b - a) * U. To get a different distribution, push U through the inverse CDF: this is called inverse transform sampling. For an exponential variable with rate r the inverse CDF is -ln(1 - u) / r.

Python Making other distributions from uniforms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Visualizing it

flowchart LR
    A["Discrete: n equal bars of height 1/n"] --> C["All outcomes equally likely"]
    B["Continuous: flat density 1/(b-a)"] --> C

Both kinds of uniform spread the probability evenly.

Where it appears

  • Simulation and games: random numbers, shuffles, random sampling.
  • Total ignorance: if a value is only known to lie in a range, a uniform model is the neutral assumption (it also serves as a non-informative prior in Bayesian statistics).
  • Rounding error: the error from rounding to the nearest unit is roughly uniform on [-0.5, 0.5].

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. X is uniform on [2, 10]. What is P(4 <= X <= 7)?

    The interval length is 3 out of a total width of 8, so the probability is 3/8.
  2. 2. What is the mean of the continuous uniform distribution on [a, b]?

    The mean sits at the midpoint, (a + b) / 2.
  3. 3. For X uniform on [0, 4], what is the density value on [0, 4]?

    The density is 1 / (b - a) = 1/4 so the total area is 4 * 0.25 = 1.
  4. 4. For a continuous uniform variable, P(X = 3) equals:

    A single point has zero width, hence zero area and zero probability.

Summary

  • Discrete uniform: n equally likely values, mean (a + b) / 2, variance (n^2 - 1) / 12.
  • Continuous uniform on [a, b]: flat density 1 / (b - a), probability equals length ratio, variance (b - a)^2 / 12.
  • A density value is not a probability; areas are.
  • Uniform random numbers are the starting point for simulating other distributions.

Next: Exponential and Geometric Distributions