How many random bits does this algoithm use on average (expected value)?

56 views Asked by At

Here, flip() is a function that returns 0 or 1 with equal probability. It can be proved function Random(n) returns a number from 0 to n-1 with identically distributed probabilities (using loop invariant).

function Random(n):
    v = 1;
    c = 0;
    loop
        v = 2v;
        c = 2c + flip();
        if (v >= n) then
            if (c < n) then
                return c;
            else
                v = v - n;
                c = c - n;
            end if
        end if
    end loop
end function

My question is how many random bits does this algorithm use in average (expected value of calling flip() function)?

I know it takes [logn]+1 times doubling v to reach n. Then with probability of n/v = n/v^([logn]+1) algorithm finishes. If it didn't finish, algorithm starts again. Here we don't know whether doubling v will cause v >= n or not and chhecking probability would be hard. How can I face this problem?

Any help is appreciated.

0

There are 0 answers