Loaded Coin Flip

Python Puzzles

Back to the Python! homepage


You need to settle a dispute by means of a coin flip.

The only coin available to you is an old wooden nickel and you are certain that it comes up heads more than 50% of the time. How can you be sure to have a fair contest that is based purely on chance by only flipping this coin?

Determine a solution to the riddle and demonstrate its accuracy via a simulation. Note, there may be more than one possible solution to this riddle.


No spoilers here! You will need to scroll down to see the answer.

import numpy as np
import pandas as pd
 
# create a binomial distribution and reshape the array
arr = np.random.binomial(size=1_000_000, n=1, p=.6)
arr2 = arr.reshape(500_000,2)
 
# create a dataframe and rename the values
df = pd.DataFrame(arr2,columns=('Coin_Flip_1','Coin_Flip_2'))
df = df.replace(0, 'T')
df = df.replace(1, 'H')
 
# determine percentage of each grouping
df['Combined'] = df['Coin_Flip_1'].str.cat(df['Coin_Flip_2'],sep="-")
series = round(df.Combined.value_counts(normalize=True),3) * 100;
 
# sort series
series.sort_values(ascending=True,inplace=True)
print(series)

When looking at the probability results of 1 million coin flips, we see that H-T and T-H have the same probability of 24%. To settle the dispute, one would call T-H or H-T. If the result was T-T or H-H, the coin would be flipped twice again.

T-T    16.0
H-T    23.9
T-H    24.0
H-H    36.0

Here we can view the results as a bar chart.

series.sort_values(ascending=True, inplace=True)
ax = series.plot.bar(title='Results of 1,000,000 Trials')
ax.set_xlabel("Coin Flip Result")
ax.set_ylabel("% Probability")
plt.savefig('loaded-histogram-barchart.png', dpi=300, bbox_inches='tight')
plt.show()