algorithmrandomprobabilityclrs

Unbiased random number generator using a biased one


You have a biased random number generator that produces a 1 with a probability p and 0 with a probability (1-p). You do not know the value of p. Using this make an unbiased random number generator which produces 1 with a probability 0.5 and 0 with a probability 0.5.

Note: this problem is an exercise problem from Introduction to Algorithms by Cormen, Leiserson, Rivest, Stein.(clrs)


Solution

  • The events (p)(1-p) and (1-p)(p) are equiprobable. Taking them as 0 and 1 respectively and discarding the other two pairs of results you get an unbiased random generator.

    In code this is done as easy as:

    int UnbiasedRandom()
    {
        int x, y;
    
        do
        {
            x = BiasedRandom();
            y = BiasedRandom();
        } while (x == y);
    
        return x;
    }