Predictive Hacks

Simulate the Birthday Problem


In probability theory, the birthday problem or birthday paradox concerns the probability that, in a set of n randomly chosen people, some pair of them will have the same birthday. In a group of 23 people, the probability of a shared birthday exceeds 50%, while a group of 70 has a 99.9% chance of a shared birthday.

You can calculate explicitly the probability of at least two people have the same birthday between n people by applying the mathematical formulas. Let’s try to estimate these probabilities numerically by applying Monte Carlo simulation. For this example, we will work in R, and we will return the estimated probability of at least two people having a common birthday by considering a group of 2 up to 300 people.

# number of simulations

# vector of people

for (people in people_vector) {
  # generate 1000 random values between 1 and 365 for each person 
  tmp<-matrix(sample(c(1:365), people*sims, replace = TRUE), ncol = people)
  # check if the number of unique bday days for each simulation is less than the number of people
  # if yes then we mean that we have a common birthday case
  prob<-mean(apply(tmp, 1, function(tmp) length(unique(tmp)))<people)
  probs<-c(probs, prob)

df<-data.frame(People=people_vector, Prob=probs)

plot(probs, main="Birthday Probability", xlab = "People", ylab = "Probability of Common Birthday")

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s