Advertisement
Guest User

23 people same birthday

a guest
Feb 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. import random
  2. from statistics import mean
  3.  
  4.  
  5. peopleCount = 21
  6. testRuns = 50000
  7. possibilities = 365  # 365 for days, 12 for months, etc.
  8. showEachResult = False
  9.  
  10.  
  11. results = []  # list of results. '1' = match, '0' = no match
  12.  
  13. for count in range (testRuns):
  14.     people = []  # Create list of people
  15.  
  16.     for x in range (peopleCount):  # Assigns random birthdays to 23 people
  17.         people.append([x, random.randint(1,possibilities)])
  18.  
  19.     talker = 0  # Talker starts at first person
  20.     listener = 1  # Listeners start at person after talker
  21.     finished = False
  22.  
  23.     while finished == False:
  24.         while listener < peopleCount:
  25.             if people[talker][1] == people[listener][1]:
  26.                 finished = True
  27.                 if showEachResult == True:
  28.                     print(str(talker) + " matches with " + str(listener) + " at " + str(people[talker][1]))
  29.                 results.append(1)
  30.                 listener = peopleCount  # Spaghetti exit
  31.             else:
  32.                 listener += 1
  33.  
  34.         talker += 1
  35.         listener = talker + 1
  36.         if talker == peopleCount:
  37.             if showEachResult == True:
  38.                 print("No matching birthdays ----------")
  39.             finished = True
  40.             results.append(0)
  41.  
  42. print("Finished")
  43.  
  44.  
  45. print("Out of " + str(peopleCount) + " people, with " + str(possibilities) + " possibilities, \n" +
  46.     str(mean(results) * 100) + "% of the " + str(testRuns) + " tests had a match")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement