Skip to content

Instantly share code, notes, and snippets.

@maximveksler
Created October 28, 2011 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximveksler/1322856 to your computer and use it in GitHub Desktop.
Save maximveksler/1322856 to your computer and use it in GitHub Desktop.
Empirical proof for Khan Academy birthday problem http://www.khanacademy.org/video/birthday-probability-problem
"""
Empirical proof for Khan Academy birthday problem http://www.khanacademy.org/video/birthday-probability-problem
I still don't understand why the calculation is using permutations so I decided to verify it is correct.
I too was having trouble convincing myself that this is the correct answer, so I wrote a python program that would randomly build a class of 30 students and check if some have colliding birth days.
What the code does is building this 30 people class again and again and then calculating in how many of the classes 2 or more students has the same birth day.
The final probability calculation is: <People with colliding birth days> / <Total number of sampled classes>
The result of a 5h execution provided as following:
From 26683392 classes tested, 70.6264143629% had at least 2 children born on the same day.
From 26683393 classes tested, 70.6264154637% had at least 2 children born on the same day.
From 26683394 classes tested, 70.6264165645% had at least 2 children born on the same day.
From 26683395 classes tested, 70.6264139177% had at least 2 children born on the same day.
From 26683396 classes tested, 70.6264150185% had at least 2 children born on the same day.
From 26683397 classes tested, 70.6264161194% had at least 2 children born on the same day.
"""
import random
COLLIDING = 0.0;
SAMPLE = 0.0;
while True:
SAMPLE = SAMPLE + 1
_30_birthdays = set()
for i in range(30):
b_day = random.randint(1,365)
_30_birthdays.add(b_day)
# A Set holds unique values, so if our birthdays set does not contain 30 values that means that
# at least 1 value appeared twice.
if(len(_30_birthdays) != 30):
COLLIDING = COLLIDING + 1
print("From " + str(int(SAMPLE)) + " classes tested, " + str((COLLIDING / SAMPLE) * 100) + "% had at least 2 children born on the same day.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment