Skip to content

Instantly share code, notes, and snippets.

@prydie
Created May 14, 2010 12:10
Show Gist options
  • Select an option

  • Save prydie/401076 to your computer and use it in GitHub Desktop.

Select an option

Save prydie/401076 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Python Script to work caculate the binomial distrobution of a given probability to a given accuracy
# By Andrew Pryde - http://www.pryde-design.co.uk
# Function for finding the factorial of a number
def fact(x):
return (1 if x==0 else x * fact(x-1))
# Function for n "Choose" r (nCr)
def nCr(n, r):
return (fact(n)/(fact(r)*fact(n-r)))
# Binomial function iteself
def B( n, p, a ):
r=0
print "R : Probability"
while r<=n:
print r, ":", round(nCr(n, r)*(p**r)*((1-p)**(n-r)), a)
r=r+1
return
# Set the variables
n = input("Please enter n: ")
p = input("Please enter p: ")
a = input("Please enter decimal accuracy: ")
# Call the function with users variables
B(n, p, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment