Skip to content
Advertisement

convert an unfair coin into a fair coin in Python 2.7

Using Python 2.7. Suppose I have an unfair coin and I want to turn it into a fair coin using the following way,

  1. Probability of generating head is equal for unfair coin;
  2. Flip unfair coin and only accept head;
  3. When a head is appearing, treat it as 1 (head for virtual fair coin), when another head is appearing, treat it as 0 (tail for virtual fair coin), next time when head appears, treat it as 1, next time treat as 0, …, and so on.

Not sure if this method works? Actually I am not quite confident about the method above and also how to use equalCoinHelper() correctly (I mark my question in my code).

If anyone have any good ideas, it will be great.

JavaScript

Advertisement

Answer

Getting a Fair Toss From a Biased Coin explains a simple algorithm for turning a biased coin into a fair coin:

  1. Flip the coin twice.
  2. If both tosses are the same (heads-heads or tails-tails), repeat step 1.
  3. If the tosses come up heads-tails, count the toss as heads. If the tosses come up tails-heads, count it as tails.

In Python this would be:

JavaScript

The elif and else blocks can be simplified to just:

JavaScript
Advertisement