# Simulating a Fair Coin with a Biased Coin

DevFeed: [Simulating a Fair Coin with a Biased Coin](<https://devfeed.tech/articles/simulating-a-fair-coin-with-a-biased-coin-40342.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2014/02/08/simulating-a-fair-coin-with-a-biased-coin/>)

Published: 2014-02-08T19:03:46Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Computer science](<https://devfeed.tech/topics/computer-science.md>), [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Python](<https://devfeed.tech/topics/python.md>)

Tags: [algebra](<https://devfeed.tech/tags/algebra.md>), [coins](<https://devfeed.tech/tags/coins.md>), [guest-post](<https://devfeed.tech/tags/guest-post.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [probabilistic](<https://devfeed.tech/tags/probabilistic.md>), [python](<https://devfeed.tech/tags/python.md>), [random-number-generators](<https://devfeed.tech/tags/random-number-generators.md>), [science](<https://devfeed.tech/tags/science.md>)

## AI overview

This tutorial explains von Neumann's method for simulating a fair coin using a biased coin. It examines pairs of independent flips, discards matching pairs, returns the first result when the pair differs, and derives the expected number of flips.

## Source excerpt

This is a guest post by my friend and colleague Adam Lelkes. Adam's interests are in algebra and theoretical computer science. This gem came up because Adam gave a talk on probabilistic computation in which he discussed this technique. Problem: Simulate a fair coin given only access to a biased coin. Solution: (in Python) def fairCoin(biasedCoin): coin1, coin2 = 0,0 while coin1 == coin2: coin1, coin2 = biasedCoin(), biasedCoin() return coin1 Discussion: This is originally von Neumann's clever idea.