Randomized algorithm

  1. How to generate 0,1 with 50% probability?? Ans:- we can think of natural number . We know that every two consecutive number is always odd and even or even and odd.

  2. Given a function rand2() that returns 0 or 1 with equal probability, write a function that returns 1 with 75% probability and 0 with 25% probability using rand2() only.

There are various kind of problem like generate rand6() from rand2() and many more. I will explain some of them here.

  1. Given a function rand6() that returns random numbers from 1 to 6 with equal probability, implement one-liner function rand12() using rand6() that returns random numbers from 1 to 12 with equal probability.

  2. Given a function rand2() that returns 0 or 1 with equal probability, implement rand3() using rand2() that returns 0, 1 or 2 with equal probability.

Generalization:- Implement randM() using randN() when M > N: Use randX() to generate randM().

Note: N^b * (randN() - 1) + N^(b - 1) * (randN() - 1) + N^(b - 2) * (randN() - 1) + ... + N^0 * (randN() - 1) generates randX() - 1, where X = N^(b + 1).