Also, for the calculation of a biased coin from an unbiased coin, I think it's worth mentioning how to do the reverse, which is a fun trick if you've never seen it before:
def fair_coin():
while True:
a = biased_coin()
b = biased_coin()
if a != b: return a
This assumes biased_coin() returns heads with some unknown but constant probability p between 0 and 1, and tails otherwise.
Also, for the calculation of a biased coin from an unbiased coin, I think it's worth mentioning how to do the reverse, which is a fun trick if you've never seen it before:
This assumes
biased_coin()returns heads with some unknown but constant probability p between 0 and 1, and tails otherwise.