Week 6: Homework Solutions

3.

"""
Verifying the Birthdays Paradox

During each trial the list 'birthdays' will be the number of 
birthdays on each day of the year: eg birthdays[0] being the 
number of birthdays on 1st Jan and birthdays[364] being the
number of birthdays on 31st Dec.
"""

import random

NO_OF_TRIALS = 100000
NO_OF_PEOPLE = 23

successes = 0

for i in range(NO_OF_TRIALS):
    # Initialize the list of birthdays to be all zeroes.
    birthdays = [0]*365

    # For each person, randomly choose their birthday.
    for person in range(NO_OF_PEOPLE):
        day = random.randrange(365)
        birthdays[day] = birthdays[day] + 1

    # See if any day has more than 1 birthday on them.
    if birthdays.count(0) + birthdays.count(1) < 365:
        successes = successes + 1

print("Proportion of successes =", successes/NO_OF_TRIALS)