Something's wrong. House can't win almost 2/3 of the time if it has 0% edge. It would mean that it is possible for the house to lose 2/3 time with 0% player edge. Something like that would be exploited by now.
You're correct, something's wrong. But what is it?
Something in the code. I'm reviewing it right now, gut feeling is something like the way those variables are incremented inside those while loops. If the code is OK (which I doubt), then we stumbled onto something very interesting.
No, it's not the code. The code is right, in that it does what I intended it to do:
It starts with a balance of 15, and martingales from 1 unit, doubling on loss, resetting on win, and stopping either when the bankroll has doubled, or when it can not afford to make the next bet.
There's a prize of 15 CLAM tipped to your Just-Dice account for the first person to post a correct explanation of how it is possible that your chance of doubling up using that strategy can be less than 50%, and for the casino's expectation to still be zero...
Found it. The trick is you have left with some money when you can not afford to make a bet, which can be accumulated with the similar leftovers from next round. Changed three lines of code to demonstrate it, added 'bankroll_left' variable to accumulate for the unaccounted funds:
#!/usr/bin/env python
import random
start = 15 # starting bankroll
target = 30 # target bankroll
base = 1 # starting bet
rounds = wins = bankroll = bankroll_left = 0
while True:
bankroll_left += bankroll # added what's left of the bankroll to a sum
bet = base
rounds += 1
while True:
if random.random() < 0.5: # we won
bankroll += bet # award winnings
bet = base # reset
if bankroll >= target: # we reached the target
wins += 1
break
else: # we lost
bankroll -= bet # subtract loss
bet *= 2 # double bet
if bet > bankroll: # we can't afford the next bet
break
if rounds % 100000 == 0:
print "%6d wins out of %6d rounds (%6.3f%%) with %6d left in total bankroll" % (wins, rounds, wins * 100.0 / rounds, bankroll_left)
And the result:
37978 wins out of 100000 rounds (37.978%) with 1500673 left in total bankroll
75740 wins out of 200000 rounds (37.870%) with 2996025 left in total bankroll
113803 wins out of 300000 rounds (37.934%) with 4497196 left in total bankroll
151835 wins out of 400000 rounds (37.959%) with 5997364 left in total bankroll
189945 wins out of 500000 rounds (37.989%) with 7501452 left in total bankroll