Monday, July 18, 2011

Python script for Birthday Paradox

Probablity of two people have same birthday is .5 .
More information checkout http://en.wikipedia.org/wiki/Birthday_problem



#!/usr/bin/python


# Function to calculate the birthday paradox

def calParadox(in_noOfdates,in_noOfBdaysPerYear):
avg =float(1)
avgnext =float(0)
count =1
curNoOfdates =noOfdates=in_noOfdates
noOfBdaysPerYear=in_noOfBdaysPerYear
avgList = []

# probablity of two people having same birthday should be equel to .5 for more information
# check wikipedia about birthday problem

while avg >float(.5):
curNoOfdates =curNoOfdates-noOfBdaysPerYear
avg =avg * curNoOfdates/noOfdates
avgList.append(avg)
count +=1


if (avgList[-2] -.5) < (.5-avg):
count -=1

return count


# Call the function with parameters of no of birthdays and days per year :)
paradoxVal =calParadox(365,1)
print "Birthday paradox is %d" % paradoxVal

No comments:

Post a Comment