What is the probability of 2 people having the same birthday?
In a group of let’s say 23 people. Exploring the Birthday Paradox with Python.

I read an interesting blog post, which said that in a group of 23 people, the probability of two people having the same birthday is %50.7.
I can not believe that. This looks surreal to me and to see whether it is true or not, I decided to open Anaconda and ran the Jupyter Notebook.
But first.
What is the Birthday Paradox?

The Birthday Paradox is a famous problem in probability theory that shows the surprising nature of probabilities.
It explores the probability of at least two people sharing a birthday within a group.
Contrary to intuition, the probability of this happening is significantly higher than most people would expect.
Now, to grab the logic behind this calculation, let’s do it by hand first.
Let’s calculate it by hand first!

The Birthday Paradox arises from the seemingly counterintuitive result that the probability of at least two people sharing a birthday in a group of 23 people is about 50%.
This result may be surprising because there are 365 days in a year, so it might seem like the odds of a shared birthday would be quite low.
To understand this paradox, we use the complementary approach, which involves calculating the probability of the opposite event: That no one in the group shares a birthday.
What is the complementary probability theorem?
The complementary probability theorem is a concept in probability theory that states the probability of an event not happening equals 1 minus the probability of the event happening.
It helps in solving problems where calculating the probability of the complementary event is easier.
In the coin toss example, the probability of not getting a “heads” is found by subtracting the probability of getting a “heads” (0.5) from 1, which results in a probability of 0.5, the same as getting a “tails”.
Once we find this probability, we can subtract it from 1 to get the probability of at least two people sharing a birthday.
Now let’s turn to our example.
Step-by-step calculation for a group of 23 people
- For the first person, there are no restrictions on their birthday, so the probability of them having a unique birthday in the group is 1 (365/365).
- For the second person, there are 364 remaining unique birthdays available (since we assume the first person already took one of them). So, the probability of the second person having a unique birthday is 364/365.
- For the third person, there are now 363 remaining unique birthdays (assuming the first two people have different birthdays). So, the probability of the third person having a unique birthday is: 363/365.
- We continue this process for all 23 people in the group. For the nth person, the probability of having a unique birthday is (365 — (n — 1)) / 365.
To calculate the probability that all 23 people have unique birthdays, we multiply the individual probabilities together:
- P(all unique) = (365/365) * (364/365) * (363/365) * … * (343/365)
- Finally, to find the probability of at least two people sharing a birthday (our original question), we subtract the probability of all unique birthdays from 1:
- P(at least one pair) = 1 — P(all unique)
General formula for any group size
The general formula for the probability of at least two people sharing a birthday in a group of n people is:
P(at least one pair) = 1 — (365/365) * (364/365) * (363/365) * … * ((365 — n + 1)/365)
By following these steps, we can calculate the probability of at least two people sharing a birthday in a group of 23 people to be approximately 50.73%.
But that might be the hard by calculate it by hand, or even with calculater. So let’s use Python as a calculater.
Calculating with Python

In this section, let’s discover how to calculate the probability of at least two people sharing a birthday in a group of 23 people using Python, as well as explore the probability with different group sizes and visualize the results.
A. Setting up the Python environment
To calculate the probability using Python, you will need a Python environment with a suitable version installed (Python 3.6 or higher is recommended). You can use a code editor, such as Visual Studio Code, or an interactive environment like Jupyter Notebook.
B. Defining a function to calculate the probability
We will define a function called birthday_paradox_probability(n) that takes an integer n as an argument, representing the number of people in the group, and returns the probability of at least two people sharing a birthday.
So you can change the number from 23 to any number you want.
def birthday_paradox_probability(n):
probability = 1
for i in range(n):
probability *= (365 - i) / 365
return 1 - probabilityC. Calculating the probability for a group of 23 people using the function
Now, we can use the birthday_paradox_probability function to calculate the probability for a group of 23 people.
group_size = 23
probability = birthday_paradox_probability(group_size)
print(f"In a group of {group_size} people, the probability of at least 2 people having the same birthday is approximately {probability * 100:.2f} percent.")Let’s see the output.

D. Exploring the probability with different group sizes and visualizing the results
We can further analyze how the probability changes with different group sizes by creating a loop that iterates through various group sizes, calculates the probability for each, and stores the results in a list. We can then use a plotting library like Matplotlib to visualize the results.
import matplotlib.pyplot as plt
group_sizes = list(range(1, 101))
probabilities = [birthday_paradox_probability(n) for n in group_sizes]
plt.plot(group_sizes, probabilities)
plt.xlabel('Group Size')
plt.ylabel('Probability')
plt.title('Probability of at Least Two People Sharing a Birthday')
plt.grid(True)
plt.show()Let’s see the output.

As you can see, if you collect 60 people, it will be 100% that two people share a birthday.
Interesting, right?
Cheat Sheets and Source Codes
If you read that far, thank you!
If you still are not a member of Medium and are eager to learn by reading, here is my referral link.
I constantly am increasing the number of Cheat Sheets and Source codes for you.
This week I created a ChatGPT cheat sheet and trust me since the ChatGPT was released, I do not remember a day that I did not use ChatGPT.
Also, you can choose one of the Cheat sheets or projects for me to send to you by filling following forms;
Here is my NumPy cheat sheet.
Here is the source code of the “How to be a Billionaire” data project.
Here is the source code of the “Classification Task with 6 Different Algorithms using Python” data project.
Here is the source code of the “Decision Tree in Energy Efficiency Analysis” data project.
Here is the source code of the “DataDrivenInvestor 2022 Articles Analysis” data project.
“Machine learning is the last invention that humanity will ever need to make.” Nick Bostrom






