If you find any of this useful, please consider donating via PayPal to help keep this site going.

# Coin Flipping in Python

7/18/19

As I note in Objections to Frequentism

The Strong Law of Large Numbers (SLLN) says that it is almost certain that between the mth and nth observations in a group of length n, the relative frequency of Heads will remain near the fixed value p, whatever p may be (ie. doesn't have to be 1/2), and be within the interval [p-e, p+e], for any small e > 0, provided that m and n are sufficiently large numbers. That is, P(Heads) in [p-e, p+e] > 1 - 1/(m*e2).

I showed this in an Excel spreadsheet here. In this article, I share Jupyter notebook code I wrote to carry out this "coin" flipping in Python.

Here are examples from flipping 500 and 1,000,000 times. For the larger number of flips, note that I zoomed in on the Y-axis

One can also flip several coins at once. Here I show 1,000 flips using 10 coins

Does this behavior happen for real coins or other objects (dice, tacks, playing cards, balls drawn from an urn)? The answer is...go try it.