Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Created January 27, 2020 23:00
Show Gist options
  • Save benjamin-chan/b5071510711c9037263b855e85133011 to your computer and use it in GitHub Desktop.
Save benjamin-chan/b5071510711c9037263b855e85133011 to your computer and use it in GitHub Desktop.
Simulate the Birthday Problem
# Ref: https://en.wikipedia.org/wiki/Birthday_problem
# Ref: https://www.scientificamerican.com/article/bring-science-home-probability-birthday-paradox/
library(magrittr)
library(dplyr)
people <- 23
simulations <- 1e5
expand.grid(id = 1:people,
sim = 1:simulations) %>%
mutate(bday = sample(365, simulations * people, replace = TRUE)) %>%
group_by(sim) %>%
summarize(anyDupes = anyDuplicated(bday) > 1) %>%
group_by(anyDupes) %>%
summarize(freq = n()) %>%
mutate(pct = freq / sum(freq))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment