Problem:
Describe an implementation of the procedure RANDOM(a, b) that only makes calls to
RANDOM(0, 1). What is the expected running time of your procedure, as a function of a and
b?
Solution:
Complexity: Θ(log (b-a+1))
Describe an implementation of the procedure RANDOM(a, b) that only makes calls to
RANDOM(0, 1). What is the expected running time of your procedure, as a function of a and
b?
Solution:
- n = ceil(log(b-a+1))
- decimal_number=0;
- do
- for i = 1 to n: get all binary
- decimal_number = generate decimal from binary digits
- while decimal_number > b-a+1
- return decimal_number
Complexity: Θ(log (b-a+1))