Skip to content

Instantly share code, notes, and snippets.

@OrDuan
Last active April 21, 2017 17:27
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 OrDuan/4575a0ffb32b3c7d1e928d78aadbf0ea to your computer and use it in GitHub Desktop.
Save OrDuan/4575a0ffb32b3c7d1e928d78aadbf0ea to your computer and use it in GitHub Desktop.
An education concept of how can you gamble and earn enough money in N rounds game.
from time import sleep
import random
game_rounds = 10
test_times = 5
base_money = 100
win_rate = 70
def log(s):
print(s)
# sleep(0.8)
total_money_records = []
bet_rate_records = []
coin_flip = [True]*int(win_rate) + [False]*int(100-win_rate)
total_invested = base_money * game_rounds * test_times
for test_i in range(test_times):
total_money = base_money
bet_rate = 0.5
for round_i in range(game_rounds):
log(f'bet rate is {bet_rate}')
is_won = random.choice(coin_flip)
log(f'is won: {is_won}')
if is_won:
bet_rate *= 2
total_money += int(total_money*bet_rate)
else:
bet_rate /= 2
total_money -= int(total_money*bet_rate)
if bet_rate > 0.5:
log(f'limiting bet to 0.5')
bet_rate = 0.5
bet_rate_records.append(bet_rate)
if total_money < 10:
raise BrokenPipeError(f'Lost all money in round {round_i*test_i}')
if bet_rate < 0.02:
raise BrokenPipeError(f'Bet rate is too small {bet_rate}, lost after {round_i*test_i}')
total_money_records.append(total_money)
print('\nSUMMERY:')
print('Avg per round:', (sum(total_money_records) - total_invested) / len(total_money_records))
print('Total money won:', sum(total_money_records) - total_invested)
print('lowest bet_rate:', min(bet_rate_records))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment