Skip to content

Instantly share code, notes, and snippets.

@Mego

Mego/budget_flip.py

Last active Jul 31, 2017
Embed
What would you like to do?
#!/usr/bin/env python3
import math
import random
successes = 0
history = []
n = random.randint(1, 256)
k = random.randint(1, n)
B = 0
while B in [0, k]:
B = random.uniform(0, k)
turn = 0
while turn < n:
print("Coins: {}".format(n))
print("Goal: {}".format(k))
print("Coins flipped: {}".format(turn))
print("Budget: {}".format(B))
print("Successes: {}".format(successes))
print("History: {}".format(history))
print()
p = eval(input("Probability for this flip: "))
flip = random.choices([0, 1], [1-p, p])[0]
print()
print("Outcome: {}".format("Success" if flip else "Failure"))
successes += flip
history.append((p, flip))
budget -= p
turn += 1
print("Coins: {}".format(n))
print("Goal: {}".format(k))
print("Coins flipped: {}".format(turn))
print("Budget: {}".format(B))
print("Successes: {}".format(successes))
print("History: {}".format(history))
print()
print("Overall Outcome: {}".format("success" if successes >= k else "failure"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment