8 FAIR COIN
John von Neumann described the procedure like this:
1. Toss the coin twice.
2. If the outcome of both coins is the same (HH or TT), start over and disregard the current toss.
3. If the outcome of both coins is different (HT or TH), take the first coin as the result and forget the second.
from random import randint
# Biased function that returns Heads with 60% probability and
# Tails with 40% probability
def toss_biased():
    # generate random number between 1-100, both inclusive
    r = randint(1, 100)
    # return T if we got number between [1-40], else return T
    return T if (r < 41) else H
def toss_unbiased():
  # difference = 0 in case of HH or TT toss again
  # difference > 0 in case of HT return  H
  # difference < 0 in case of TH return T
  difference = toss_biased() - toss_biased()
  if difference > 0:
    return H
  if difference < 0:
    return T
  return toss_unbiased()
# Generate Fair Results from a Biased Coin
if __name__ == '__main__':
    # Head
    H = 1
    # Tail
    T = 0
    head_count = tail_count = 0
     for i in range(10000):
        val = toss_unbiased()
        if val == 1:
            head_count += 1
        else:
            tail_count += 1
    print("HEADS% ~", head_count / 100, "%")     # ~50%
    print("TAILS% ~", tail_count / 100, "%")     # ~50%
Output :
HEADS% ~ 50.21 %
TAILS% ~ 49.79 %
Again run then output:
HEADS% ~ 49.95 %
TAILS% ~ 50.05 %