Two players, $A$ and $B$, have two starting amounts of money $a$ and $b$ respectively. For each round of the game, a coin with probability of heads $p$ is flipped. If it turns up heads, player $A$ gives player $B$ a dollar. Winning the game is defined as having
%matplotlib inline
import matplotlib.pyplot as plt
from random import random
a = 4 # player a's starting dollar amount
b = 6 # player b's starting dollar amount
p = 0.50 # probability that player a wins a round
# p = 0.5 represents a fair coin
original_a = a
original_b = b
a_wins = 0
b_wins = 0
trials = 100000
prob_a_wins = []
for i in range(trials):
while a != 0 and b != 0:
if random() < p:
a += 1
b -= 1
else:
a -= 1
b += 1
if b == 0:
a_wins += 1
else:
b_wins += 1
prob_a_wins.append(1.0 * a_wins / (a_wins + b_wins))
a = original_a
b = original_b
plt.plot(prob_a_wins)
plt.ylabel("Estimated P(A wins)")
plt.xlabel("Trial Number")
plt.show()
print("P(A wins) = " + str(prob_a_wins[-1]))
P(A wins) = 0.3996