# This program simulates the following process: several people # announce their birthdays until two of them share a birthday. # We assume that no one is born of Feburary 29th (sorry!), # and that every day is equally likely to be someone's birthday. # The function birthday() counts the number of people before # we get a match. The function makehist(n) runs n trials of # this process and then tabulates the distribution. # Are you surprised by how small the values are? import random def birthday(): birthdays = [] while True: newday = random.randint(0,364) if newday not in birthdays: birthdays.append(newday) else: return len(birthdays) + 1 def makehist(n): histcount = {} for _ in range(n): b = birthday() if b in histcount: histcount[b] += 1 else: histcount[b] = 1 keys = [i for i in histcount] keys = sorted(keys) hist = [(key, histcount[key]) for key in keys] return hist print makehist(100)