started birthday paradox
This commit is contained in:
parent
534d6ba5da
commit
210cf4418c
23
birthdayparadox.py
Normal file
23
birthdayparadox.py
Normal file
@ -0,0 +1,23 @@
|
||||
""" 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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user