You are given rand7, a function that returns a uniformly random integer from 1 to 7. Write rand10(rand7) that returns a uniformly random integer from 1 to 10, calling only rand7 for randomness.
- Combine two calls into one number from 1 to 49:
index = (first - 1) * 7 + second, where first is the earlier call - If
index <= 40, return 1 + (index - 1) % 10. Otherwise throw the pair away and draw two new calls - The result must be exactly uniform, so do not add two calls, and do not reduce a number from 1 to 49 with
% 10 alone - There is no upper limit on the number of calls, but the expected number is about 2.45