Skip to content

Instantly share code, notes, and snippets.

@yakkomajuri
Last active April 20, 2020 23:00
Show Gist options
  • Save yakkomajuri/852eeb2194fdda1d61827b09a3462df8 to your computer and use it in GitHub Desktop.
Save yakkomajuri/852eeb2194fdda1d61827b09a3462df8 to your computer and use it in GitHub Desktop.
Simple implementation of a program to explain the birthday paradox
from tqdm import trange
from time import sleep
import random
def main():
n_people = int(input())
iterations = int(input())
print("\n\n\n")
matches = 0
for _ in trange(iterations):
sleep(0.1)
generated_bdays = []
for _ in range(n_people):
month = random.randint(1,12)
max_day = 30 if month in [4,6,9,11] else 31
if month == 2:
max_day = 28
day = random.randint(1,max_day)
bday = str(day) + "/" + str(month)
if bday in generated_bdays:
matches = matches + 1
break
else:
generated_bdays.append(bday)
print("\nResult: " + str(matches) + "/" + str(iterations))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment