From 9830f6b04be9d97ea46074f26de960645c0ed975 Mon Sep 17 00:00:00 2001 From: cheeks <134818917+leftovertoast@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:41:32 +0000 Subject: [PATCH] completed birthdayparadox.py --- birthdayparadox.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/birthdayparadox.py b/birthdayparadox.py index 2a06ece..2fc8d17 100644 --- a/birthdayparadox.py +++ b/birthdayparadox.py @@ -21,6 +21,7 @@ def getBirthdays(numberOfBirthdays): birthdays.append(birthday) return birthdays + def getMatch(birthdays): """Returns the date object of a birthday that occurs more than once in the birthdays list.""" @@ -33,8 +34,10 @@ def getMatch(birthdays): if birthdayA == birthdayB: return birthdayA # Return the matching birthday + # Display the intro: print('''Birthday Paradox, by Al Sweigart al@inventwithpython.com + The Birthday Paradox shows us that in a group of N people, the odds that two of them have matching birthdays is surprisingly large. This program does a Monte Carlo simulation (that is, repeated random @@ -42,6 +45,7 @@ simulations) to explore this concept. (It's not actually a paradox, it's just a surprising result.) ''') + # Set up a tuple of month names in order: MONTHS = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') @@ -62,8 +66,46 @@ for i, birthday in enumerate(birthdays): # Display a comma for each birthday after the first birthday. print(', ', end='') monthName = MONTHS[birthday.month - 1] - dateText = '{} {}'.format(monthName, match.day) + dateText = '{} {}'.format(monthName, birthday.day) print(dateText, end='') print() print() +# Determine if there are two birthdays that match. +match = getMatch(birthdays) + +# Display the results: +print('In this simulation, ', end='') +if match != None: + monthName = MONTHS[match.month - 1] + dateText = '{} {}'.format(monthName, match.day) + print('multiple people have a birthday on', dateText) +else: + print('there are no matching birthdays.') +print() + +# Run through 100,000 simulations: +print('Generating', numBDays, 'random birthdays 100,000 times...') +input('Press Enter to begin...') + +print('Let\'s run another 100,000 simulations.') +simMatch = 0 # How many simulations had matching birthdays in them. +for i in range(100_000): + # Report on the progress every 10,000 simulations: + if i % 1000 == 0: + print(i, 'simulations run...') + birthdays = getBirthdays(numBDays) + if getMatch(birthdays) != None: + simMatch = simMatch + 1 +print('100,000 simulations run.') + +# Display simulation results: +probability = round(simMatch / 100_000 * 100, 2) +print('Out of 100,000 simulations of', numBDays, 'people, there was a a') +print('matching birthday in that group', simMatch, 'times. This means') +print('that', numBDays, 'people have a', probability, '% chance of') +print('having a matching birthday in their group.') +print('That\'s probably more than you would think!') + + +#if __name__ == '__main__':