16 · Uniform distribution
Equal density everywhere on [a, b] — the maximum-entropy distribution on a bounded interval and the workhorse of random initialization.
Uniform(a, b) places equal density on every point in [a, b] — the maximum-entropy distribution on a bounded interval.
Without this:
Without it, you can't sample fairly within a range — the random-init step of nearly every algorithm depends on uniform draws.
The Uniform distribution Uniform(a, b) models complete ignorance about where in [a, b] a value lies: every sub-interval of the same length has the same probability.
PDF: f(x) = 1 / (b − a) for x ∈ [a, b], else 0
The PDF is a horizontal rectangle — constant height, zero outside [a, b].
Parameters:
- a — lower bound
- b — upper bound
- scipy convention:
scipy.stats.uniform(loc=a, scale=b−a)— the second parameter is the width, not the upper bound.
Mean and variance:
- E[X] = (a + b) / 2 — the midpoint
- Var(X) = (b − a)² / 12
CDF: F(x) = (x − a) / (b − a) for x ∈ [a, b] F(a) = 0, F(b) = 1 — a simple linear ramp from 0 to 1.
Discrete uniform (briefly): if you roll a fair die, each of {1, 2, 3, 4, 5, 6} has probability 1/6 — that is the discrete version.
The inverse-transform sampling trick: Because the Uniform CDF is the identity on [0, 1], you can simulate any distribution F by:
- Draw u ~ Uniform(0, 1)
- Return x = F⁻¹(u)
This is why rng.uniform(0, 1) is the most fundamental RNG primitive — every other distribution's sampler can be derived from it.
Python (in browser)
scipy.stats.uniform(loc=a, scale=b−a) — note the second argument is the width (b−a), not b itself. Mean = (a+b)/2 = 5. Var = (b−a)²/12 = 100/12 ≈ 8.33. PDF = 1/(b−a) = 0.1 everywhere in [0, 10]. CDF grows linearly from 0 to 1.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
100 000 samples from Uniform(0, 10) form an approximately flat histogram — every x-value in [0, 10] is equally likely. The red dashed line at y=0.1 is the theoretical PDF. Open /tmp/uniform.png to view.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
The inverse-transform trick — draw u ~ Uniform(0,1), return F⁻¹(u) — is the theoretical foundation of every random sampler. In NumPy all distributions ultimately reduce to uniform draws under the hood.
Var(Uniform(0, 12)) = ?
- **PDF**: f(x) = 1/(b−a) on [a,b], 0 elsewhere. **Mean** = (a+b)/2, **Var** = (b−a)²/12. CDF is a linear ramp from 0 to 1.
- Scipy parameterisation: `uniform(loc=a, scale=b−a)` — second argument is width, not upper bound.
- Inverse-transform sampling: draw u ~ Uniform(0,1), return F⁻¹(u) — the theoretical foundation of every random sampler.
Random initialization (weights, dropout masks), train/test split row selection, MCMC starting points.
If you remove it: No way to fairly randomize anything — every reproducibility experiment uses uniform draws under the hood.