Source of Book
For the textbook, see the note in Github.
Ex5-1-2
Q
Describe an implementation of procedure Random(a,b) that only makes calls to Random(0,1).
What is the expected running time of your proceedure, as a function of a and b?
Understand of Q (and changes)
I think the question asks me to implement Random(a,b) only with Random(0,1) (Here, Random(0,1) generates a random number from 0 to 1 (inclusive) (i.e. 0 <= random number <= 1 ) ). And then describe the proceedure of the implementation. Finally, it asks the running time of your proceedure.
In my answer (shown in My Answer section ) of this article, it only answers the requirements of this section instead what the question in textbook asks (shown in Q section).
Answers
My Answer
Implementation 1
Code
C++
// only call Random(0,1).
double Random(int a, int b)
{
int interval = b - a ;
double randomNumber = (double) Random(0,1);
double result = (double) ( randomNumber * (double) interval + (double) a );
return result;
}
Expected Running Time
Suppose 1 call of Random(0,1) takes r times which takes longer time than all basic operations ( * , + ).
Then 1 call of Random(a,b) will take:
r + time of a few basic operations. Ideally, it approximates to r.
Other Answer
From the website,