This gamling strategy that I heard somewhere stuck with me:

1. Bet one dollar on heads in a coin flip
2. Flip a coin
3. If you win, profit and restart! If you loose. Double your bet on heads

So by doubling up your bet each time you loose you’re hoping to win back what you lost next time. Could it be that easy? Probably not. Let’s try it with some code!

A roulette wheel is as close to a coin flip we will get in a casino. So let’s try that. First we need a function acting roulette wheel:

def spinn_wheel():
result = randrange(37)

if result == 0: # Green
return "G"
if result in range(1,19): # Red
return "R"
if result in range(19,36): # Black
return "B"


We set up some variables and libraries that we will use

balance = 0 # hopefully this will be positive
stake = 1 # start at 1 as bet
bet = "B" # always bet on black

from random import randrange
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


Let’s make 100 bets and follow the strategy:

for i in range(100):

color = spinn_wheel()

if color == bet:
balance += stake
stake = 1
else:
balance -= stake
stake = 2*stake

plt.scatter(i, balance, s=10)

plt.show()


print balance


31

So after 100 bets we are at plus. But look at that, we where down -250 on a loosing streak where we had to keep doubling our bet to get back on track. Looks like a loosing streak of about 8. What’s the probability of that?

print 0.5**8


0.00390625

0.3 percent does not sound to much. And 250 is no problem actually.

How about a loosing streak of 12. How much do we need to get us out of that situation?

print 2**12


4096

Well, it’s starting to hurt.

print 2**13


8192

print 2**14


16384

print 2**15
to_much = 2**15


32768

Ok, 32768 would at least for me be alot more than I could manage to get in cash to get me out of the situation (if in dollars). So a loosing streak of 15 is really bad. How probable is that to happend?

print 0.5**15


3.0517578125e-05

Not very probable. So let’s try the above strategy a thousand times to try to make some money:

balances = [] # let's log every end balance

for i in range(1000):
balance = 0
stake = 1
j = 0

while True:
j += 1
color = spinn_wheel()

if stake > to_much:
balances.append(balance)
break

# let's take home the win if a
# a hundred test passes and we're
# on plus
if j > 100 and balance > 0:
balances.append(balance)
break

if color == bet:
balance += stake
stake = 1
else:
balance -= stake
stake = 2*stake

print balances


[47, 40, 51, 39, 41, 48, 52, 57, 8, 42, 43, 44, 37, 35, 39, 49, 12, 46, 51, 49, 41, 56, 48, 50, 42, 45, 42, 46, 41, 50, 47, 35, 55, 44, 42, 56, 38, 50, 36, 41, 40, 42, 45, 43, 46, 26, 49, 41, 47, 38, 39, 39, 49, 55, 50, 39, 47, 34, 44, 30, 47, 43, 48, 53, 29, 49, 43, 45, 45, 44, 40, 46, 41, 40, 39, 49, 39, 40, 50, 39, 53, 37, 42, 43, 38, 42, 25, 49, 44, 39, 48, 45, 47, 48, 39, 41, 50, 45, 43, 38, 50, 37, 49, 46, 38, 54, 50, 51, 42, 42, 37, 44, 39, 44, 48, 51, 37, 12, 52, 42, 43, 37, 53, 51, 44, 49, 47, 47, 49, 40, 51, 51, 41, 40, 47, 47, 40, 46, 42, 39, 37, 47, 43, 32, 47, 50, 43, 37, 39, 42, 50, 46, 48, 42, 53, 35, 39, 48, 42, 57, 43, 44, 46, 44, 44, 50, 46, 42, 45, 43, 51, 40, 44, 47, 40, 30, 49, 35, 45, 9, 43, 50, 46, 18, 56, 42, 53, 45, 45, 44, 47, 49, 45, 51, 47, 30, 45, 48, 49, 44, 49, 47, 37, 40, 45, 39, 53, 41, 54, 37, 49, 46, 38, 49, 46, 49, 46, 55, 45, 61, 33, 48, 45, 47, 44, 43, 47, 47, 42, 33, 49, 41, 42, 45, 47, 38, 45, 49, 44, 34, 41, 48, 45, 44, 24, 47, 47, 41, 50, 46, 45, 43, 49, 40, 49, 45, 51, 46, 46, 41, 36, 44, 46, 50, 47, 45, 48, 36, 38, 33, 41, 44, 38, 35, 36, 41, 43, 45, 53, 53, 59, 49, 35, 44, 46, 40, 44, 42, 45, 43, 49, 55, 46, 43, 50, 45, 63, 48, 40, 46, 49, 39, 48, 33, 53, 35, 53, 46, 44, 53, 50, 41, 38, 50, 43, 48, 45, 33, 30, 34, 42, 46, 50, 49, 38, 41, 45, 39, 45, 46, 31, 45, 40, 48, 42, 58, 48, 36, 44, 48, 37, 40, 48, 47, 42, 41, 39, 43, 51, 15, 49, 30, 46, 47, 49, 44, 44, 52, 50, 45, 50, 46, 48, 29, 50, 47, 45, 40, 40, 49, 44, 41, 38, 44, 48, 48, 44, 49, 46, 40, 43, 48, 45, 45, 37, 47, 51, 48, 49, 41, 43, 48, 41, 39, 40, 42, 51, 41, -65521, 40, 44, 50, 45, 53, 38, 56, 50, 49, 49, 34, 40, 52, 45, 51, 51, 47, 42, 37, 44, 44, 49, 44, 48, 48, 47, 38, 50, 44, 28, 42, 60, 37, 45, 51, 44, 49, 45, 39, 46, 40, 38, 40, 46, 45, 43, 46, 26, 56, 44, 34, 28, 45, 38, 37, 49, 48, 38, 47, 56, 43, 50, 41, 40, 45, 37, 49, 40, 35, 55, 35, 41, 43, 46, 42, 40, 43, 51, 49, 59, 44, 35, 45, 42, 42, 12, 43, 47, 45, 48, 52, 22, 52, 45, 49, 56, 32, 47, 43, 53, 43, 46, 41, 53, 41, 46, 34, 46, 46, 46, 45, 45, 52, 42, 50, 44, 46, 50, 48, 38, 36, 43, 43, 56, 49, 51, 54, 38, 36, 37, 40, 46, 37, 34, 34, 47, 53, 39, 45, 52, 49, 48, 42, 52, 39, 50, 42, 41, 41, 46, 49, 34, 41, 46, 50, 22, 48, 42, 47, 51, 46, 45, 49, 37, 36, 39, 47, 46, 37, 39, 52, 35, 47, 40, 51, 47, 47, 22, 35, 44, 47, 45, 41, 44, 44, 43, 50, 41, 40, 49, 39, 43, 45, 48, 45, 43, 37, 26, 51, 46, 42, 43, 47, 52, 42, 44, 39, 36, 46, 42, 32, 38, 43, 52, 42, 15, 23, 38, 45, 43, 50, 48, 38, -65528, 46, 56, 53, 49, 57, 47, 54, 53, 51, 44, 45, 41, 39, 35, 36, 50, 44, 42, 45, 43, 37, 49, 45, 54, 48, 10, 45, 49, 38, 51, 46, 52, 48, 33, 50, 52, 43, 38, 46, 43, 45, 47, 45, 58, 48, 49, 47, 50, 40, 36, 46, 30, 44, 49, 43, 44, 51, 48, 54, 51, 38, 36, 45, 43, 59, 49, 46, 44, 41, 34, 48, 41, 9, 43, 33, 56, 53, 49, 48, 34, 47, 32, 47, 46, 48, 28, 26, 27, 34, 37, 42, 45, 35, 44, 31, 46, 42, 35, 35, 47, 41, 55, 47, 32, 48, 44, 42, 48, 45, 35, 55, 46, 45, 45, 49, 47, 46, 42, 39, 44, 49, 38, 39, 33, 40, 39, 33, 41, 37, 42, 36, 47, 47, 39, 42, 36, 32, 43, 44, 44, 51, 51, 43, 32, 48, 46, 50, 48, 31, 43, 40, 50, 51, 39, 48, 44, 43, 47, 40, 42, 43, 42, 38, 44, 38, 43, 52, 36, 39, 47, 44, 50, 48, 36, 45, 45, 45, 45, 41, 55, 43, 36, 48, 44, 44, 43, 41, 51, 49, 9, 44, 47, 42, 14, 43, 45, 30, 48, 40, 48, 46, 53, 42, 29, 44, 35, 53, 42, 44, 39, 41, 45, 51, 38, 39, 53, 48, 44, 42, 43, 38, 38, 3, 54, 36, 36, 44, 42, 49, 41, 43, 50, 41, 39, 47, 46, 57, 52, 36, 52, 46, 49, 40, 38, 37, 44, 41, 36, 49, 43, 48, 35, 38, 41, 54, 46, 37, 51, 34, 45, 45, 39, 40, 38, 34, 53, 45, 32, 40, 45, 50, 47, 27, 44, 37, 45, 45, 40, 35, 39, 52, 52, 51, 43, 44, 33, 15, 50, 47, 48, 41, 48, 46, 49, 46, 37, 39, 47, 50, 51, 42, 44, 42, 44, 43, 39, 37, 52, 40, 40, 47, 26, 45, 43, 51, 33, 37, 44, 48, 49, 49, 60, 38, 49, 58, 15, 48, 46, 42, 47, 48, 52, 42, 49, 38, 40, 43, 44, 45, 36, 36, 45, 39, 39, 51, 55, 51, 49, 47, 44, 41, 45, 33, 40, 38, 38, 37, 51, 25, 37, 44, 47, 50, 55, 47, 44, 41, 51, 44, 38, 44, 35, 41, 29, 38, 48, 43]

Well, that’s not good. We are on our way to make us some profit, and the bam all our savings are gone!

print sum(balances)


-87704

So even that the probability to hit the loosing streak is low. Hiting it a thousand times will make it pretty probable.

Turns out this is a well studied problem called the Martingale betting system, and the expected outcome is indeed zero or negative in a casino where the house has an edge. Good to know instead of ending up selling all you have to break even.