+ 1
How to start this kind of questions solution?
Describe an algorithm that takes a real number 0 ≤ p ≤ 1 as input, and returns an independent random bit that is equal to 1 with the given probability p, using independent fair coin flips as the only source of randomness. What is the expected running time of your algorithm (as a function of p)?
1 Respuesta
0
Suppose for a moment that, instead of coin flips, your source of randomness was a real number between 0 and 1. We'll call it r. Then an easy way to get 1s with a probability of p is to generate random r, then compare r with p. If r <= p then return 1, else return 0.
Make sure you understand that before proceeding.
Now how to use coin flips to do the same thing? I have an idea that should work: use coin flips to generate r. Treat the coin flips as 1s and 0s. It is arbitrary, but say head is 1, tail is 0. Take 8 coin flips and shift the results into an unsigned byte. Now you have a random 8-bit number between 0 and 255 with uniform distribution. Divide by 255 (floating point division) and you get the equivalent of r from above. Can you figure out the rest?
Addendum: This can be slightly improved. Instead of comparing floating point numbers r and p, compare the unsigned byte directly with the integer result of 255*p.