On this page
Sometimes you need random numbers from a density f that is easy to write down but has no simple recipe for drawing from it. Rejection sampling, also called the accept-reject method, solves this with a sampler you already have and one inequality. The rejection sampling article on Wikipedia notes that John von Neumann used the algorithm and traces the idea back to Buffon's needle.
How does rejection sampling work?
You need three things: the target density f, a proposal density g you can already sample from, and a constant M with f(x) ≤ M·g(x) for every x. Then repeat:
Draw x from g.
Draw u uniformly between 0 and 1, independently of x.
If u ≤ f(x)/(M·g(x)), accept x and stop. Otherwise reject it and go back to step 1.
That is the whole algorithm, as set out in the Wikipedia entry and in Chapter II, section 3 of Luc Devroye's Non-Uniform Random Variate Generation (Springer, 1986), free on the author's page.
Why do the accepted points follow f?
Picture darts. Drawing x from g and a height y = u·M·g(x) scatters points evenly under the curve M·g. The test u ≤ f(x)/(M·g(x)) is the same as y ≤ f(x), so you keep exactly the darts under f, and darts spread evenly under f land near x in proportion to the height f(x).
In symbols, the chance of proposing a value near x and accepting it is g(x)dx × f(x)/(M·g(x)) = f(x)dx/M. The g cancels. Adding this over all x gives a total acceptance probability of 1/M, because f integrates to 1. Dividing, the accepted x has density exactly f.
Each try is independent, so the number of tries until the first success follows a geometric distribution with success probability p. The Wikipedia derivation puts it plainly: the number of proposals needed is geometric with probability 1/M, which has mean M.
Worked example: sampling the semicircle density
Take the semicircle density f(x) = (2/π)√(1 − x²) on [−1, 1]; the factor 2/π makes the half disc's area π/2 into 1. The proposal is uniform on [−1, 1], g(x) = 1/2. Using the acceptance rate 1/M from the Wikipedia derivation:
Find M. The ratio f(x)/g(x) = (4/π)√(1 − x²) is largest at x = 0, where it equals 4/π ≈ 1.273. So M = 4/π is the smallest valid bound.
Simplify the test. f(x)/(M·g(x)) = √(1 − x²), so you accept when u ≤ √(1 − x²). Both sides are non-negative, so square them: accept when x² + u² ≤ 1.
Read off the acceptance rate: 1/M = π/4 ≈ 0.785, so about 78.5% of tries are kept. Geometrically, (x, u) is uniform on a rectangle of area 2 and you keep the upper half disc, area π/2.
Try two by hand. With x = 0.6 and u = 0.7, x² + u² = 0.36 + 0.49 = 0.85, so accept 0.6. With x = −0.9 and u = 0.5, the sum is 0.81 + 0.25 = 1.06, so reject and draw again.
On average, 1,000 accepted samples cost 1,000 × 4/π ≈ 1,273 proposals.
Why does a loose bound M waste samples?
Any M ≥ 4/π is valid, but the Wikipedia article advises keeping M as small as possible while f(x) ≤ M·g(x) still holds, since an M closer to 1 means fewer rejections. Suppose you skip the calculus and take M = 2, so the envelope M·g is a flat line at height 1 instead of 2/π. The acceptance rate drops to 1/2, and 1,000 samples now need about 2,000 proposals, roughly 57% more than 1,273. Each rejection wastes two random draws and an evaluation of f.
M can never go below the largest value of f/g, so to do better, choose a g shaped more like f. The cost also grows with dimension: darts in a square hit the inscribed disc π/4 of the time, but in ten dimensions the ball fills about 0.0025 of the cube, one dart in 400. For high dimensions the same article points to Markov chain Monte Carlo instead, which rests on the stationary distribution of a Markov chain. Another route keeps every draw and reweights it: importance sampling.
How do you design a rejection sampler?
Pick a g you can sample, nonzero wherever f is, with tails at least as heavy as f.
Set M to the maximum of f(x)/g(x): differentiate, set to zero, check the endpoints.
Simplify f(x)/(M·g(x)) before coding it, as √(1 − x²) above.
Predict the acceptance rate 1/M. If your run keeps a very different fraction, look for a bug or a broken bound.
Finding M is a Class 12 maxima problem. If derivatives or continuous densities still feel shaky, the Fermi Concept Library covers every concept in mathematics with its own lesson. To practise the maximisation step, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
Next time you meet a density with no obvious recipe, look for an easy g, find the smallest M, and start throwing darts.



