Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Last active September 21, 2021 19:08
Show Gist options
  • Save tshirtman/3dac768401e8a696d59b2bb9240437a3 to your computer and use it in GitHub Desktop.
Save tshirtman/3dac768401e8a696d59b2bb9240437a3 to your computer and use it in GitHub Desktop.
from random import choice
from decimal import Decimal
stats = [0] * 100
def advance(n, f):
return n * (1+f)
def backoff(n, f):
return n * (1-f)
def solve(n):
return Decimal((1 - n) / n)
def play():
n = Decimal(0.00000001)
success = False
for i in range(100):
f = solve(n)
n = choice((advance, backoff))(n, f)
if n == 1 and not success:
success = True
stats[i] += 1
# print(f"got there in {i} turns")
# print(f"game ended, value is {n}")
return n == 1
def check_odds(n):
wins = 0
losses = 0
for i in range(n):
if play():
wins += 1
else:
losses += 1
print(f"observed odds over {n} tries {wins}/{n} = {wins/n}")
def display_stats():
norm = max(stats)
scale = 100 / norm
print(stats)
for i, s in enumerate(stats):
print(f"{i:2d} {'=' * int(s * scale)}")
if __name__ == '__main__':
check_odds(100_000)
display_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment