- Actually a really interesting problem.
- Bitwise is easy to understand but it’s not the most efficient one.
- Construct a function to return
0, 1with 50%, 50% possibility.
[1, 6]odd num returns 0, and even num returns 1.
- For 10, we only need
- We can get num from
- We want num between
[1, 10], so we only need
[0, 9]then add 1 to the num.
- Do while loop when
n > 9.
Given the API
rand7() that generates a uniform random integer in the range
[1, 7], write a function
rand10() that generates a uniform random integer in the range
[1, 10]. You can only call the API
rand7(), and you shouldn’t call any other API. Please do not use a language’s built-in random API.
Each test case will have one internal argument
n, the number of times that your implemented function rand10() will be called while testing. Note that this is
not an argument passed to
Input: n = 3