Skip to content

Instantly share code, notes, and snippets.

@xenic
Created December 12, 2018 04:10
Show Gist options
  • Save xenic/3fd879d68bf0784476e6aeeafdf0c857 to your computer and use it in GitHub Desktop.
Save xenic/3fd879d68bf0784476e6aeeafdf0c857 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from math import factorial
def choose(n,k):
return factorial(n) / (factorial(k) * factorial(n-k))
def get_inputs():
trials = int(input("Number of flips: "))
heads_count = int(input("At least of heads: "))
heads_prob = float(input("Percent chance heads: "))
return (trials, heads_count, heads_prob)
if __name__ == "__main__":
trials, heads_count, heads_prob = get_inputs()
c_prob = 0
for n in range(trials, heads_count -1, -1):
#probability of getting exactly n in flips trials, given heads_prob
p = choose(trials, n) * heads_prob**trials
c_prob += p
print(f'given the probability of getting a heads is {heads_prob}\n'
f'probability of getting exactly {n} heads in {trials} trials = {p}.\n'
f'probability of getting {n} or more heads in {trials} trials = {c_prob}.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment