Birthday Problem

fun
python
R
Author
Affiliation

Massachusetts Institute of Technology

Published

June 13, 2020

We have all been there, classic math riddle:

How many people need to be in one room so that the probability of two of them having the same birthday is more than 0.5?

In a recent bootcamp exercise we tackled this in python and I wanted to share, just because it’s fun. I did it for a range of probabilities. My solution is probably not the fastest/shortest/most pythonic, but it’s a little thing I put out there so, if you want to use/improve it, please do!

import numpy as np
import matplotlib.pyplot as plt


def prob(n):
    numerator = np.math.factorial(365) / np.math.factorial(365-n)
    denominator = 365 ** n
    return(1 - numerator/denominator)

probs = list(map(prob, range(100)))

plt.plot(range(len(probs)), probs)
plt.show()

A bad translation into R, it would be something like:

prob <- function(n){
  numerator = exp(lfactorial(365) - lfactorial(365-n))
    denominator = 365 ** n
    return(1 - numerator/denominator)
}

probs = sapply(0:99, function(n) prob(n))

plot(0:99, probs, type="l",
     main="Probability of 2 people having same birthday",
     xlab = "People in a room", ylab="probability")

Reuse

Citation

BibTeX citation:
@online{andina2020,
  author = {Andina, Matias},
  title = {Birthday {Problem}},
  date = {2020-06-13},
  url = {https://matiasandina.com/posts/2020-06-13-birthday-problem},
  langid = {en}
}
For attribution, please cite this work as:
Andina, Matias. 2020. “Birthday Problem.” June 13, 2020. https://matiasandina.com/posts/2020-06-13-birthday-problem.

Enjoy my creations?

I'm so glad you're here. As you know, I create a blend of fiction, non-fiction, open-source software, and generative art - all of which I provide for free.

Creating quality content takes a lot of time and effort, and your support would mean the world to me. It would empower me to continue sharing my work and keep everything accessible for everyone.

How can you support my work?

There easy ways to contribute. You can buy me coffee, become a patron on Patreon, or make a donation via PayPal. Every bit helps to keep the creative juices flowing.



Become a Patron!

Share the Love

Not in a position to contribute financially? No problem! Sharing my work with others also goes a long way. You can use the following links to share this post on your social media.

Affiliate Links

Please note that some of the links above might be affiliate links. At no additional cost to you, I will earn a commission if you decide to make a purchase.


© CC-By Matias Andina, 2023 | This page is built with ❤️ and Quarto.