Created:October 25, 2024
Last Updated:May 2, 2025
This calculator helps you compute the probabilities of a uniform distribution given the parameters a and b. You can find the probability of values being equal to, less than, greater than, or between specific values. The distribution chart shows the probability density function (PDF) of the uniform distribution, which models scenarios where all outcomes in a range have an equal probability of occurring.
Calculator
Parameters
Distribution Chart
Click Calculate to view the distribution chart
Related Calculators
Learn More
Uniform Distribution: Definition, Formula, and Applications
Uniform Distribution
Definition: The uniform distribution describes a probability distribution where all outcomes in an interval are equally likely to occur. It is one of the simplest continuous probability distributions and serves as a basis for many random number generation techniques.
Formula:The probability density function (PDF) and cumulative distribution function (CDF) are given by:
Where:
- is the lower bound of the distribution
- is the upper bound of the distribution
Properties
- Mean (Expected Value):
- Variance:
- Entropy: , which is the maximum entropy for any random variable on $[a,b]$.
- Support:
- Constant probability density over the interval
- Symmetric around the mean
How to Calculate Uniform Distribution in R
R
library(tidyverse)
# Parameters
a <- 2 # lower bound
b <- 6 # upper bound
x1 <- 3 # lower comparison point
x2 <- 5 # upper comparison point
# calculate P(x1 ≤ X ≤ x2)
p_between <- punif(x2, min = a, max = b) - punif(x1, min = a, max = b)
print(str_glue("P({x1} ≤ X ≤ {x2}) = {round(p_between, 4)}")) # P(3 ≤ X ≤ 5) = 0.5
# calculate probability density at x1
density_at_x1 <- dunif(x1, min = a, max = b)
print(str_glue("f({x1}) = {round(density_at_x1, 4)}")) # f(3) = 0.25
# calculate cumulative probability at x1
cdf_at_x1 <- punif(x1, min = a, max = b)
print(str_glue("P(X ≤ {x1}) = {round(cdf_at_x1, 4)}")) # P(X ≤ 3) = 0.25
# mean and variance
mean <- (a + b) / 2
variance <- (b - a)^2 / 12
print(str_glue("Mean: {round(mean, 4)}")) # Mean: 4.0
print(str_glue("Variance: {round(variance, 4)}")) # Variance: 1.3333
# plot
x <- seq(a - 0.5, b + 0.5, length.out = 1000)
density <- dunif(x, min = a, max = b)
df <- tibble(x = x, density = density)
ggplot(df, aes(x = x, y = density)) +
geom_line() +
geom_area(data = subset(df, x >= x1 & x <= x2),
aes(x = x, y = density),
fill = "blue", alpha = 0.3) +
geom_vline(xintercept = c(x1, x2),
linetype = "dashed", color = "red") +
labs(title = str_glue("Uniform Distribution U({a}, {b})"),
subtitle = str_glue("P({x1} ≤ X ≤ {x2}) = {round(p_between, 4)}"),
x = "x",
y = "Density") +
theme_minimal()
How to Calculate Uniform Distribution in Python
Python
import numpy as np
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
# Parameters
a = 2 # lower bound
b = 6 # upper bound
x1 = 3 # lower comparison point
x2 = 5 # upper comparison point
# Calculate probability P(x1 ≤ X ≤ x2)
p_between = stats.uniform.cdf(x2, loc=a, scale=b-a) - stats.uniform.cdf(x1, loc=a, scale=b-a)
print(f"P({x1} ≤ X ≤ {x2}) = {p_between:.4f}")
# Calculate probability density at x1
pdf_at_x1 = stats.uniform.pdf(x1, loc=a, scale=b-a)
print(f"P(X = {x1}) = {pdf_at_x1:.4f}")
# Calculate cumulative probability at x1
cdf_at_x1 = stats.uniform.cdf(x1, loc=a, scale=b-a)
print(f"P(X ≤ {x1}) = {cdf_at_x1:.4f}")
# mean and variance
mean = (a + b) / 2
variance = (b - a)**2 / 12
print(f"Mean: {mean:.4f}")
print(f"Variance: {variance:.4f}")
# Create plot
x = np.linspace(a - 0.5, b + 0.5, 1000)
pdf = stats.uniform.pdf(x, loc=a, scale=b-a)
# Plot
plt.figure(figsize=(10, 6))
plt.plot(x, pdf, 'b-', label='PDF')
# Shade area between x1 and x2
x_shade = x[(x >= x1) & (x <= x2)]
pdf_shade = stats.uniform.pdf(x_shade, loc=a, scale=b-a)
plt.fill_between(x_shade, pdf_shade, alpha=0.3, color='blue')
# Add vertical lines at x1 and x2
plt.axvline(x=x1, color='red', linestyle='--', alpha=0.5)
plt.axvline(x=x2, color='red', linestyle='--', alpha=0.5)
# Add labels and title
plt.title(f'Uniform Distribution U({a}, {b})')
plt.xlabel('x')
plt.ylabel('Density')
plt.grid(True, alpha=0.3)
plt.show()