Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Created June 7, 2024 21:38
Show Gist options
  • Save nicoguaro/a496fe82cc58fe315fac445180deba93 to your computer and use it in GitHub Desktop.
Save nicoguaro/a496fe82cc58fe315fac445180deba93 to your computer and use it in GitHub Desktop.
Flip a fair coin 100 times—it gives a sequence of heads (H) and tails (T). For each HH in the sequence of flips, Alice gets a point; for each HT, Bob does, so e.g. for the sequence THHHT Alice gets 2 points and Bob gets 1 point. Who is most likely to win?
# -*- coding: utf-8 -*-
"""
Flip a fair coin 100 times—it gives a sequence of heads (H)
and tails (T). For each HH in the sequence of flips, Alice
gets a point; for each HT, Bob does, so e.g. for the
sequence THHHT Alice gets 2 points and Bob gets 1 point.
Who is most likely to win?
https://x.com/littmath/status/1769044719034647001
@author: Nicolás Guarín-Zapata
@date: June 2024
"""
import numpy as np
np.random.seed(45)
nrounds = 100000
nflips = 100
bob_wins = 0
alice_wins = 0
ties = 0
for rounds in range(nrounds):
bob = 0
alice = 0
flips = np.random.randint(0, 2, nflips)
for cont in range(nflips - 1):
consecutive_flips = list(flips[cont:cont + 2])
if consecutive_flips == [0, 1]:
bob += 1
if consecutive_flips == [0, 0]:
alice += 1
if bob > alice:
bob_wins += 1
elif bob < alice:
alice_wins += 1
else:
ties += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment