#!/usr/bin/env python from mpmath import * import math import cgitb import cgi cgitb.enable() form = cgi.FieldStorage() try: n = int(float(form.getvalue('n','365'))) if n<=0: n=365 except: n = 365 try: x = int(float(form.getvalue('x','10'))) if x<=0: x = 10 if x>1e9: x = 10 except: x = 10 s = form.getvalue('s', None) def collisionProbability(nKeys, nEntities): t1 = mpf(1) for i in range(nKeys-nEntities+1, nKeys+1): t1 *= i return 1 - t1 * power(mpf(1)/nKeys,nEntities) print ('''Content-type: text/html jclement.ca - birthday paradox solver

This is a web based birthday paradox solver.

Example 1:

What is the probability that any two students, in a class of 20 people, will share a birthday?

Range of Identifiers: 365 (days in a year that birthday could lie on)

Number of Subjects: 20 (students in class)

Will yield a probility of two students sharing a birthday of about 42%.

Example 2:

In my database of approximately 20,000 customers what is the probability of two customers being assigned the same completely random 20 digits identifier?

Range of Identifiers: 1E20 (10^20 possible identifiers)

Number of Subjects: 20000 (customers)

Will yield a probility of two customers being assigned the same identifier as 0.0000000003%.

Calculator:

''') print ('''
Range of Identifiers:
Number of Subjects:
''' % (n,x)) if s: print('''

Probability of two subjects with identical identifier is %0.10f%%

''' % (100 * collisionProbability(n,x))) print ('''
''')