Whenever a small group of people get together, many are often surprised if two people
have the same birthday. Karma? Fate? Some "energy field" of the universe? (No, no and no.)
Perhaps this shouldn't be so surprising since there are only 365
possible birthdays in a year. If person #1's birthday is on day X, then person #2 has a
1/365 chance of sharing #1's birthday. Person #3's chances rise to 2/365, and so on. Our
point? These numbers, which are the chances of birthdays being the same, aren't that small!
All told, would you believe that if 23 people are together, the odds that two
of them share a birthday are about 50%? Let's test this here.
Let's use random numbers to determine if a given day is someone's birthday. Our plan will be to loop
through N people (you choose N), and use a random number to determine the day of the
year (1..365) of their birthday. Using the hist[day]=hist[day]+1 line, we'll count the
number of people whose birthday falls on a given day.
Run the program many times (like 20, 30, or 50). Think of each run as a "get together" of N people. After
each run, count how many times two people
share a birthday. If the screen ends up blank, it means a pair did not share a birthday. If your N=23, it should happen for around 50% of your runs.
You can review arrays here
and histograms here
Move the mouse over a dotted box for more information.
Now you try.
Choose some number of people in the N= line and fix the day=math.random
line to choose a day of the year between 1 and 365.
Type your code here:
See your results here:
Show a friend, family member, or teacher what you've done!
Here is a share link to your code:
Does your code work? Want to run it on your iPhone?
Here's your code:
Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.
Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into
Then click the "Use on iPhone" button that you'll see.