Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from statistics import mean
- peopleCount = 21
- testRuns = 50000
- possibilities = 365 # 365 for days, 12 for months, etc.
- showEachResult = False
- results = [] # list of results. '1' = match, '0' = no match
- for count in range (testRuns):
- people = [] # Create list of people
- for x in range (peopleCount): # Assigns random birthdays to 23 people
- people.append([x, random.randint(1,possibilities)])
- talker = 0 # Talker starts at first person
- listener = 1 # Listeners start at person after talker
- finished = False
- while finished == False:
- while listener < peopleCount:
- if people[talker][1] == people[listener][1]:
- finished = True
- if showEachResult == True:
- print(str(talker) + " matches with " + str(listener) + " at " + str(people[talker][1]))
- results.append(1)
- listener = peopleCount # Spaghetti exit
- else:
- listener += 1
- talker += 1
- listener = talker + 1
- if talker == peopleCount:
- if showEachResult == True:
- print("No matching birthdays ----------")
- finished = True
- results.append(0)
- print("Finished")
- print("Out of " + str(peopleCount) + " people, with " + str(possibilities) + " possibilities, \n" +
- str(mean(results) * 100) + "% of the " + str(testRuns) + " tests had a match")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement