There is known Random(0,1)
function, it is a uniformed random function, which means, it will give 0 or 1, with probability 50%. Implement Random(a, b)
that only makes calls to Random(0,1)
What I though so far is, put the range a-b in a 0 based array, then I have index 0, 1, 2...b-a.
then call the RANDOM(0,1)
b-a times, sum the results as generated idx. and return the element.
However since there is no answer in the book, I don't know if this way is correct or the best. How to prove that the probability of returning each element is exactly same and is 1/(b-a+1)
?
And what is the right/better way to do this?