On this page
A computer's random number generator gives you numbers spread evenly between 0 and 1. Simulations need waiting times, loaded dice and decay times. Inverse transform sampling is the simplest bridge from one to the others, and its proof is one line.
What is inverse transform sampling?
Given a cumulative distribution function F and a uniform random variable U on [0, 1], the random variable X = F⁻¹(U) has distribution F (Wikipedia: inverse transform sampling). It is also called the inversion method, and it is the first method in the general-principles chapter of Luc Devroye's book Non-Uniform Random Variate Generation (Springer-Verlag, 1986, now free online from the author).
The recipe has three steps:
Write down the CDF, F(x) = P(X ≤ x).
Solve u = F(x) for x. That is F⁻¹(u).
Draw U, return F⁻¹(U).
On a graph, U picks a height; you walk across to the CDF curve and drop down to the x-axis. Where the curve is steep, many heights land on a short stretch of x, which is exactly where the density is high.
Why does X = F⁻¹(U) work?
Because F only ever goes up, F⁻¹(U) ≤ x happens exactly when U ≤ F(x). So
P(X ≤ x) = P(F⁻¹(U) ≤ x) = P(U ≤ F(x)) = F(x).
The last step is just the definition of uniform: the chance that U is at most a number u in [0, 1] is u. X therefore has CDF F, which is what we wanted. For CDFs with flat parts or jumps, define F⁻¹(u) as the smallest x with F(x) ≥ u, and the same argument goes through (Wikipedia: inverse transform sampling).
How do you generate exponential random numbers from uniform ones?
The exponential distribution with rate λ has F(x) = 1 − e^(−λx) for x ≥ 0. Set u = 1 − e^(−λx) and solve: e^(−λx) = 1 − u, so x = −ln(1 − u)/λ. The sampler is
X = −ln(1 − U)/λ.
Worked example: requests reach a server at λ = 2 per minute, so the mean gap is 1/λ = 0.5 minutes.
U = 0.10 gives X = −ln(0.90)/2 ≈ 0.053 minutes.
U = 0.50 gives X = −ln(0.50)/2 ≈ 0.347 minutes, the median.
U = 0.90 gives X = −ln(0.10)/2 ≈ 1.151 minutes.
Most gaps are short and a few are long, the shape you see in why exponential waiting times are memoryless. Chain these gaps together and you have simulated a Poisson process of random arrivals.
Why 1 − U and not U? Since 1 − U is also uniform, −ln(U)/λ works just as well in theory (Wikipedia). In code, though, Python's random() returns a float in the range 0.0 ≤ x < 1.0 (Python random module docs). U can be exactly 0, and ln(0) is undefined, while 1 − U is never 0.
How do you sample a discrete distribution, like a loaded die?
For a discrete distribution, the CDF is just a running total of the probabilities (Wikipedia). Suppose a die shows 6 with probability 0.5 and each of 1 to 5 with probability 0.1. The running totals are:
face 1: 0.1
face 2: 0.2
face 3: 0.3
face 4: 0.4
face 5: 0.5
face 6: 1.0
Rule: return the first face whose running total is greater than U. U = 0.37 gives face 4, because 0.37 is past 0.3 but under 0.4. U = 0.62 gives face 6. Half of the interval [0, 1) belongs to face 6, so a 6 comes up half the time.
What if the inverse CDF has no formula?
The method needs F⁻¹, and many distributions do not hand you one. For the normal distribution there is no analytical expression for the quantile function, so other methods such as the Box–Muller transform may be preferred (Wikipedia: inverse transform sampling). Box–Muller, published by G. E. P. Box and Mervin E. Muller in The Annals of Mathematical Statistics in 1958, turns two uniform numbers into two independent normal ones.
You have three practical choices:
Invert numerically. Solve F(x) = U with bisection or Newton's method, or use a library quantile function such as NormalDist.inv_cdf in Python's statistics module. The same Wikipedia article notes that the normal quantile can be approximated very accurately with polynomials.
Use a special transform built for that distribution, such as Box–Muller for the normal.
Use rejection sampling, which needs only the density, not the CDF (Devroye covers it alongside inversion).
Once you can sample any distribution, the next question is how to sample cleverly when the event you care about is rare; that is importance sampling for rare events.
If CDFs, quantiles and the exponential distribution still feel shaky, the Fermi Concept Library covers every concept in mathematics, so you can fill the gap before the next step. To practise inverting CDFs yourself, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



