Skip to content

Instantly share code, notes, and snippets.

@itdxer
Last active May 24, 2021 11:19
Show Gist options
  • Save itdxer/1d8f3ae5585671261475154aaa2d297e to your computer and use it in GitHub Desktop.
Save itdxer/1d8f3ae5585671261475154aaa2d297e to your computer and use it in GitHub Desktop.
Simulation that shows that with a fair roulette wheel Martingale betting system still has a 50% probability of doubling initial amount of money
import random
N = 50
p_win = 0.5
num_experiments = 100000
def play_game(N):
assert N >= 0
target_N = 2 * N
# Keep playing until you either lost all of your money or doubled your initial amount
while 0 < N < target_N:
assert N >= 0
next_bet = 1 # always bet 1 at the beggining
start_N = N
while next_bet <= N: # Check if we have enough money to make a bet
current_bet = next_bet
N -= current_bet
assert N >= 0
# Spin the roulette wheel and observe the outcome (in terms of win or lose)
won = random.random() <= p_win
if won:
N += 2 * current_bet
assert N == (start_N + 1) # we always win +1
break
else:
# Follow the strategy. When we lose, double the amount of money and try again
next_bet *= 2
assert N in (0, target_N)
return N != 0
outcomes = [play_game(N) for _ in range(num_experiments)]
print(sum(outcomes) / num_experiments) # observed average success rate
print((1 - 1/N) ** N) # invalid prediction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment