Skip to content

Instantly share code, notes, and snippets.

@gpiancastelli
Created January 31, 2011 14:30
Show Gist options
  • Save gpiancastelli/804094 to your computer and use it in GitHub Desktop.
Save gpiancastelli/804094 to your computer and use it in GitHub Desktop.
Calculate the probability of the birthday problem for 23 people on a user-supplied number of randomly generated samples.
#!/usr/bin/env python2.6
# In probability theory, the birthday problem, or birthday paradox pertains to
# the probability that in a set of randomly chosen people some pair of them
# will have the same birthday. By the pigeonhole principle, the probability
# reaches 100% when the number of people reaches 367 (including February 29
# births). But perhaps counter-intuitively, 99% probability is reached with
# just 57 people, and 50% probability with 23 people. These conclusions are
# based on the assumption that each day of the year (except February 29) is
# equally probable for a birthday.
#
# (See also: http://en.wikipedia.org/wiki/Birthday_paradox.)
#
# This program calculates the probability with 23 people, based on a
# user-supplied number of randomly generated samples.
import random
import sys
from os.path import basename
if len(sys.argv) != 2:
print 'Usage: %s samples' % basename(sys.argv[0])
sys.exit(1)
if not sys.argv[1].isdigit():
print 'Error: %s is not an integer' % sys.argv[1]
sys.exit(2)
SAMPLES = int(sys.argv[1])
PEOPLE = 23
count = 0
for i in xrange(SAMPLES):
birthdays = [random.randint(1, 365) for p in range(PEOPLE)]
if len(set(birthdays)) < PEOPLE:
count += 1
print '%.1f' % (count * 100.0 / SAMPLES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment