bbsopp/birthdayparadox.py
2025-03-13 17:18:57 +00:00

23 lines
876 B
Python

""" Birthday Paradox Simulation, by Al Sweigart al@inventwithpython.com
Explore the surprising possibilities of the "Birthday Paradox".
More info at https://en.wikipedia.org/wiki/Birthday_problem
View this code at https://nostarch.com/big-book-small-python-projects
Tags: short, math, simulation"""
import datetime, random
def getBirthdays(numberOfBirthdays):
"""Returns a list of number random date objects for birthdays."""
birthdays = []
for i in range(numberOfBirthdays):
# The year is unimportant for our simulation, as long as all
# birthdays have the same year.
startOfYear = datetime.date(2001, 1, 1)
# Get a random day into the year:
randomNumberOfDays = datetime.timedelta(random.randint(0, 364))
birthday = startOfYear + randomNumberOfDays
birthdays.append(birthday)
return birthdays