5.1-1:
Since we can compare between any two elements, we know the total order on the ranks of the candidates.
5.1-2:
RANDOM(a,b)
%range = b - a;
%d = the number of binary bits %range has
do
for(%i = 1 to %d):
%i th bit of integer %res = rand(0,1);
while(%res > %range);
return (a + %res);
Each time of while loop need O(log(b-a)) time, and the expectation of that while loop is constant. Therefore, it takes time O(log(b-a))
5.1-3:
UNBIASED-RANDOM()
do:
a = random(0,1);
b = random(0,1);
while(a==b);
return a;
The possibility for (1,0) is p(1-p), and the same for (0,1).
Thus, the possibility for the termination of while loop is 2p(1-p);
the expectation is 1/(2p(1-p))
Thus the expected running time is O(1/(2p(1-p)))
I've spent some time thinking about how to find this expectation:
Suppose the possibility for event e happen is p, then the exception is: