10/17
Part 5 of 7 · Rejection samplingLearn and do

Build a fair 1 to 7 from a 1 to 5 by throwing some draws away

LeetCode's problem 470 asks for a uniform random number from 1 to 10 given one from 1 to 7, and follows up with the expected number of calls. Here's the same question one size down: a fair 1 to 7 from rand5, a fair 1 to 5.

Predict

Is (rand5() + rand5()) % 7 + 1 a fair 1 to 7?

Pick an answer above to see the rest
Predict

How many calls to rand5 does one rand7 take on average?

Pick one to see what it prints.
Pick an answer above to see the rest