Skip to content

Instantly share code, notes, and snippets.

@trevorknight
Last active December 19, 2015 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorknight/69cd4d76cd1eeccc8714 to your computer and use it in GitHub Desktop.
Save trevorknight/69cd4d76cd1eeccc8714 to your computer and use it in GitHub Desktop.
If you play roulette and always bet on red and your strategy is to always bet $1 unless you lose in which case you double your previous bet.
import random
NUMBER_OF_SIMULATIONS = 1000
STARTING_BANK_BALANCE = 1000
ODDS_OF_LOSS = 20.0/38.0
NUMBER_OF_PLAYS = 180
bankrupt_results = dict()
successful_results = []
for simulation in range(NUMBER_OF_SIMULATIONS):
balance = STARTING_BANK_BALANCE
wager = 1
for play in range(NUMBER_OF_PLAYS):
#print(str.format("Wagering {}", wager))
won = random.random() < ODDS_OF_LOSS
#print("Won." if won else "Lost.")
balance = balance + wager if won else balance - wager
#print(str.format("New balance: {}", balance))
wager = 1 if won else min(2 * wager, balance)
if (balance <= 0):
#print(str.format("Ran out of money after {} plays!", play))
bankrupt_results[play] = bankrupt_results[play] + 1 if play in bankrupt_results else 1
break;
if balance > 0:
#print(str.format("Ended with {} after {} plays", balance, NUMBER_OF_PLAYS))
successful_results.append(balance)
if (simulation + 1) % 50 == 0:
print(str.format("Finished {} simulations.", simulation + 1))
print("Results:")
print(str.format("{} successful results out of {}.", len(successful_results), NUMBER_OF_SIMULATIONS))
print(str.format("With an average successful final balance of {}.", float(sum(successful_results)/len(successful_results))))
print(str.format("Average final balance (expected return) on this strategy: {}", float(sum(successful_results)/NUMBER_OF_SIMULATIONS)))
if len(bankrupt_results.values()) > 0:
print("Bankrupcy results. Bankrupt after: ")
print('Plays \t occurances')
for p, o in bankrupt_results.iteritems():
print(str.format('{} \t {}', p, o))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment