Simulating Random Variables Using Uniform Distribution in Python
Learn how to generate samples of both discrete and continuous random variables by mapping uniformly distributed numbers onto target distributions, with step-by-step explanations, probability tables, and Python NumPy code that simulates events and computes frequencies for large-scale experiments.
Simulation of Discrete Random Variables
Using uniformly distributed random numbers, we can simulate a discrete random variable whose probability mass function is given. By dividing the interval [0,1] into sub‑intervals whose lengths equal the probabilities, the event that the uniform sample falls into a sub‑interval corresponds to the occurrence of the associated outcome.
Problem 1
Given a trial with three possible events A, B, C having probabilities 0.2, 0.3, and 0.5 respectively, simulate 100 000 trials and compute the empirical frequencies.
We generate 100 000 uniform random numbers and count how many fall into each probability interval (0‑0.2, 0.2‑0.5, 0.5‑1). Dividing the counts by 100 000 yields the estimated frequencies.
Code
<code>from numpy.random import rand
import numpy as np
n=100000; a=rand(n); n1=np.sum(a<=0.2)
n2=np.sum((a>0.2) & (a<=0.5)); n3=np.sum(a>0.5)
f=np.array([n1,n2,n3])/n; print(f)</code>Problem 2
For ten events A₁…A₁₀ with probabilities 0.2, 0.05, 0.01, 0.06, 0.08, 0.1, 0.3, 0.05, 0.03, 0.12, simulate 10 000 trials and report the counts for each event.
The cumulative probabilities are used to define interval boundaries. A uniform sample is compared against these boundaries to determine which event occurs.
Code
<code>from numpy.random import rand
import numpy as np
n=10000; a=rand(n)
p=np.array([0.2,0.05,0.01,0.06,0.08,0.1,0.3,0.05,0.03,0.12])
cp=np.cumsum(p); c=[]; c.append(np.sum(a<=cp[0]))
for i in range(1,len(p)):
c.append(np.sum((a>cp[i-1]) & (a<=cp[i])))
print(c)</code>Simulation of Continuous Random Variables
Uniform random numbers on an interval can also be transformed to obtain samples from any continuous distribution whose cumulative distribution function (CDF) is invertible. If U~Uniform(0,1) and F is the target CDF, then X = F⁻¹(U) follows the desired distribution.
Problem 3
Generate samples from an exponential distribution.
Let U be uniform on (0,1). The inverse CDF of the exponential distribution with rate λ is X = -ln(1‑U)/λ, which can be computed directly in Python.
Modeling
In practice, NumPy provides functions such as numpy.random.exponential that generate exponential samples without manual transformation.
Reference
司守奎,孙玺菁 Python数学实验与建模
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.