Advertisement
Lyuben_Andreev

BirthdayParadox

Jul 13th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | Source Code | 0 0
  1. import datetime
  2. import random
  3.  
  4. def getBirthdays(numberOfBirthdays):
  5.     birthdays = []
  6.     for i in range(numberOfBirthdays):
  7.         startOfYear = datetime.date(2001, 1, 1)
  8.         randomNumberOfDays = datetime.timedelta(random.randint(0, 364))
  9.         birthday = startOfYear + randomNumberOfDays
  10.         birthdays.append(birthday)
  11.     return birthdays
  12.  
  13. def getMatch(birthdays):
  14.     if len(birthdays) == len(set(birthdays)):
  15.         return None
  16.     for a, birthdayA in enumerate(birthdays):
  17.         for b, birthdayB in enumerate(birthdays[a + 1:]):
  18.             if birthdayA == birthdayB:
  19.                 return birthdayA
  20.  
  21. print('''Birthday Paradox,
  22.  The birthday paradox shows us that in a group of N people, the odds
  23.  that two of them have matching birthdays is surprisingly large.
  24.  This program does a Monte Carlo simulation (that is, repeated random
  25.  simulations) to explore this concept.
  26. (It's not actually a paradox, it's just a surprising result.)
  27. ''')
  28.  
  29. MONTHS = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  30.           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
  31.  
  32. while True:
  33.     print('How many birthdays shall I generate? (Max 100)')
  34.     response = input("> ")
  35.     if response.isdecimal() and (0 < int(response) <= 100):
  36.         numBDays = int(response)
  37.         break
  38. print()
  39.  
  40. print(f'Here are {numBDays} birthdays:')
  41. birthdays = getBirthdays(numBDays)
  42. for i, birthday in enumerate(birthdays):
  43.     if i != 0:
  44.         print(', ', end='')
  45.     monthName = MONTHS[birthday.month - 1]
  46.     dateText = '{} {}'.format(monthName, birthday.day)
  47.     print(dateText, end='')
  48. print()
  49. print()
  50.  
  51. match = getMatch(birthdays)
  52. print('In this simulation, ', end='')
  53. if match is not None:
  54.     monthName = MONTHS[match.month - 1]
  55.     dateText = f'{monthName} {match.day}'
  56.     print('multiple people have a birthday on', dateText)
  57. else:
  58.     print('there are no matching birthdays.')
  59. print()
  60.  
  61. print('Generating', numBDays, 'random birthdays 100,000 times...')
  62. input('Press Enter to begin...')
  63. print('Let\'s run another 100,000 simulations.')
  64. simMatch = 0  # How many simulations had matching birthdays in them.
  65.  
  66. for i in range(100000):
  67.     if i % 10000 == 0:
  68.         print(i, 'simulations run...')
  69.     birthdays = getBirthdays(numBDays)
  70.     if getMatch(birthdays) is not None:
  71.         simMatch += 1
  72. print('100,000 simulations run.')
  73.  
  74. probability = round(simMatch / 100000 * 100, 2)
  75. print(f'Out of 100,000 simulations of {numBDays} people, there was a')
  76. print(f'matching birthday in that group {simMatch} times. This means')
  77. print(f'that {numBDays} people have a {probability}% chance of')
  78. print('having a matching birthday in their group.')
  79. print('That\'s probably more than you would think!')
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement