Implement Rand10() Using Rand7()
Given an API rand7() that returns a uniformly random integer in [1,7], implement rand10() that returns a uniformly random integer in [1,10]. You may only call rand7() and must not use any other random source.
Open official problem prompt ↗Turn a fair 7-sided die into a fair 10-sided die using only calls to the 7-sided die, with no bias.
Rolling two 7-sided dice to fill a 7x7 grid of 49 squares, then keeping only 40 of them (four full sets of ten) and re-rolling whenever you land on one of the 9 leftover squares.
- Input
- n = 1 (generate one value)
- Output
- [2]
- Why
- rand10() returns a single uniformly random integer in [1,10]; 2 is one valid outcome, each value having probability 1/10.
1 <= n <= 10^5 (n = number of rand10 calls the judge makes)rand7() is uniform over [1,7]Only rand7() may be used as a randomness source