TExcelTAD
Home › NumPy › Uniform Distribution

Uniform Distribution

In a uniform distribution every value in a range is equally likely. Its density is flat, a rectangle instead of a bell. It is the simplest continuous distribution and the building block for many others: computers first generate uniform numbers and then transform them into other shapes.

Parameter Meaning Default
low lower bound (included) 0.0
high upper bound (excluded) 1.0
size output shape one value

Generating uniform data

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
print(rng.uniform(size=4).round(3))                # in [0, 1)
print(rng.uniform(-1, 1, size=4).round(3))         # in [-1, 1)
wait = rng.uniform(0, 10, size=5)                  # minutes until a bus that comes every 10 min
print(wait.round(1))
[0.774 0.439 0.859 0.697]
[-0.812  0.951  0.522  0.572]
[1.3 4.5 3.7 9.3 6.4]

rng.random() is the same as rng.uniform(0, 1). For whole numbers with equal probability, use rng.integers(low, high) instead: that's the discrete uniform distribution (like a fair die).

Mean and spread

For a uniform distribution on [a, b), the mean is the midpoint (a + b) / 2 and the standard deviation is (b − a) / √12:

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
a, b = 2, 8
x = rng.uniform(a, b, size=200_000)
print(f"mean {x.mean():.3f}  (theory {(a + b) / 2})")
print(f"std  {x.std():.3f}  (theory {(b - a) / np.sqrt(12):.3f})")
print(f"share below 5: {np.mean(x < 5):.3f}")
mean 5.000  (theory 5.0)
std  1.732  (theory 1.732)
share below 5: 0.500

Because the density is flat, the share of values below 5 should match the share of the range below 5: (5 − 2) / (8 − 2) = 0.5.

Visualising

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
x = rng.uniform(0, 10, size=20_000)
plt.figure(figsize=(6, 3.5))
plt.hist(x, bins=20, density=True, color="#4dabcf", edgecolor="white")
plt.axhline(1 / 10, color="black", linestyle="--", label="theoretical density 0.1")
plt.xlabel("value")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted a histogram of 20000 uniform values in [0, 10)")
Plotted a histogram of 20000 uniform values in [0, 10)

All bars have about the same height, matching the flat density of 1 / (10 − 0) = 0.1.

Example: estimating π

Drop random points into a 1×1 square. The fraction that lands inside the quarter circle of radius 1 approaches its area, π/4. This is a classic Monte Carlo method:

python · try it yourself
import numpy as np

rng = np.random.default_rng(2025)
for n in (100, 10_000, 1_000_000):
    x = rng.uniform(0, 1, n)
    y = rng.uniform(0, 1, n)
    inside = (x**2 + y**2) < 1
    print(f"n={n:>9,}: pi ~ {4 * inside.mean():.4f}")
n=      100: pi ~ 3.0400
n=   10,000: pi ~ 3.1388
n=1,000,000: pi ~ 3.1415

More points give a better estimate, although slowly: the error shrinks roughly with 1/√n.

Test yourself

Which values can rng.uniform(3, 5) return?

  1. Integers 3, 4
  2. Any float from 3 up to (not including) 5
  3. Only 3 or 5
  4. Floats from 0 to 5

What is the mean of a uniform distribution on [0, 20)?

  1. 0
  2. 10
  3. 20
  4. 5

What does a histogram of uniformly distributed data look like?

  1. A bell curve
  2. Roughly flat bars
  3. A steep drop from left to right
  4. One tall bar