Skip to content

Mapping a uniform draw to a discrete law

Question

You have a generator of uniform U(0,1)U(0,1) numbers and a discrete distribution taking values x1,…,xnx_1,\dots,x_n with probabilities p1,…,pnp_1,\dots,p_n. How do you produce samples from that distribution?

Short answer

Inverse transform on the cumulative probabilities. Precompute ck=p1+⋯+pkc_k=p_1+\dots+p_k, draw uu, and return the first xkx_k with u≤cku\le c_k. A linear scan costs O(n)O(n) per draw; binary search on the cumulative array gives O(log⁡n)O(\log n), and Walker's alias method gives O(1)O(1) per draw after O(n)O(n) setup.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.2, p.157; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question

Start with a free lesson: Normal Distribution is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.

Read it free