Chapter 8: Uniform Distribution
1. What is the Uniform distribution really?
The uniform distribution is the simplest and most intuitive continuous probability distribution.
It means:
Every value inside a certain interval is equally likely to occur.
There are no favorite values — no peak, no bell shape, no skew. It’s completely flat between the minimum and maximum.
There are two main flavors we care about:
- Continuous uniform → any real number between a and b is equally likely
- Discrete uniform → equally likely integers (like rolling a fair die)
Key parameters (only two):
- a = minimum value (lower bound)
- b = maximum value (upper bound)
The probability density is constant: f(x) = 1 / (b − a) for a ≤ x ≤ b f(x) = 0 everywhere else
2. Mental picture — how it looks
Imagine you drop a dart completely randomly on a number line between 10 and 30. Every single point between 10 and 30 has exactly the same chance of being hit.
The histogram looks like a perfect rectangle:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
┌─────────────────────────────┐ │ │ │ │ │ │ ← flat = uniform │ │ └─────────────────────────────┘ 10 30 |
3. Generating uniform random numbers in NumPy
NumPy gives you two very commonly used functions:
A. np.random.rand() — standard uniform [0, 1)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Single number print(np.random.rand()) # 1D array print(np.random.rand(8)) # 2D array — very common shape print(np.random.rand(5, 6)) # Large array for visualization u = np.random.rand(100000) |
B. np.random.uniform(low, high, size=…) — custom interval
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Between 0 and 100 (like random percentages) print(np.random.uniform(0, 100, 10).round(1)) # Between -5 and 5 (symmetric around zero) symmetric = np.random.uniform(-5, 5, 50000) # Temperatures between 18°C and 32°C daily_temps = np.random.uniform(18, 32, 365) |
4. Visualizing the uniform distribution (very important)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5)) # Standard uniform [0,1) sns.histplot(np.random.rand(80000), bins=60, stat="density", color="skyblue", alpha=0.8, ax=ax1) ax1.set_title("Uniform distribution [0, 1)", fontsize=13) ax1.set_xlabel("Value") ax1.set_ylabel("Density") ax1.set_xlim(0, 1) # Custom uniform [15, 45] custom = np.random.uniform(15, 45, 80000) sns.histplot(custom, bins=60, stat="density", color="teal", alpha=0.8, ax=ax2) ax2.set_title("Uniform distribution [15, 45]", fontsize=13) ax2.set_xlabel("Value") ax2.set_ylabel("Density") plt.tight_layout() plt.show() |
What you should always see:
- Flat top (almost perfect rectangle when sample size is large)
- Sharp drop to zero outside [a, b]
- Density height = 1 / (b − a)
5. Very common real-world use cases (you will meet these often)
Use case 1 – Generating random test values / mock data
|
0 1 2 3 4 5 6 7 8 9 10 |
# Random prices between 9.99 and 99.99 prices = np.random.uniform(9.99, 99.99, size=500) # Random percentages / probabilities probabilities = np.random.rand(1000) # [0,1) |
Use case 2 – Random initialization / starting points
|
0 1 2 3 4 5 6 7 8 9 10 |
# Random starting weights for simple models weights = np.random.uniform(-0.1, 0.1, size=(100, 30)) # Random split points for time series cross-validation split_points = np.random.uniform(0.1, 0.9, size=50) |
Use case 3 – Simulating uniform sensor readings / background noise
|
0 1 2 3 4 5 6 7 8 9 |
# Background noise in some measurement device (uniform between -2 and +2) noise = np.random.uniform(-2, 2, size=10000) signal = 50 + noise |
Use case 4 – Random sampling of categories / discrete uniform
|
0 1 2 3 4 5 6 7 8 9 10 11 |
options = ['A', 'B', 'C', 'D', 'E'] random_choices = np.random.choice(options, size=10000) sns.countplot(x=random_choices, palette="Set2") plt.title("Discrete uniform – equal probability") plt.show() |
6. Important properties & formulas (write these down)
| Property | Value / Formula |
|---|---|
| Range | [a, b] or [0, 1) for rand() |
| Mean (expected value) | (a + b) / 2 |
| Variance | (b − a)² / 12 |
| Standard deviation | (b − a) / √12 |
| Probability density | 1 / (b − a) inside interval |
| Cumulative distribution | (x − a) / (b − a) for a ≤ x ≤ b |
Quick check:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
a, b = 15, 45 data = np.random.uniform(a, b, 100000) print("Mean (should be ~30) :", data.mean().round(3)) print("Std (should be ~8.66) :", data.std().round(3)) print("Theoretical std :", (b - a) / np.sqrt(12)) |
7. Common student questions & confusions
Q: Is uniform the same as random?
No. Uniform means equal probability density in the interval. “Random” is a much broader term — normal, binomial, exponential are also random, but not uniform.
Q: Why do we often see np.random.rand() instead of uniform(0,1)?
rand() is just a convenient shortcut for uniform(0,1). They give exactly the same numbers (when seeded the same).
Q: Why does the histogram never look perfectly flat?
Because it’s a sample. With 100 values → looks bumpy. With 100,000 values → almost perfect rectangle.
Summary – Uniform Distribution Quick Reference
| Property | Value / Formula |
|---|---|
| Shape | Flat rectangle |
| Defined by | a (min), b (max) |
| Mean | (a + b)/2 |
| Variance | (b − a)² / 12 |
| NumPy standard [0,1) | np.random.rand(size) |
| Custom interval | np.random.uniform(a, b, size) |
| Most common use cases | mock data, initialization, test values, background noise, random sampling |
Final teacher advice
Whenever you need something “completely fair / no preference / equal chance” within a range → think uniform.
Whenever you see a flat histogram in real data (rare but happens in some sensors, quantization, etc.) → think uniform or discrete uniform.
Where would you like to go next?
- Difference between continuous vs discrete uniform
- How uniform is used in random number generation algorithms
- Transforming uniform to other distributions (inverse transform sampling)
- Comparing uniform vs normal vs exponential side by side
- Realistic mini-project: generate mock customer data + prices + timestamps
Just tell me what interests you most right now! 😊
