Skip to content

Instantly share code, notes, and snippets.

@tshddx
Created March 28, 2012 23:42
Show Gist options
  • Save tshddx/2231556 to your computer and use it in GitHub Desktop.
Save tshddx/2231556 to your computer and use it in GitHub Desktop.
What are the odds of, in a set of X people, there being at least Y people who share a birthday?
# Thomas Shaddox
# https://github.com/baddox
# Assumes birthdays are independent (i.e. uniformly distributed)
days = 365
ppl = 290 # I'm so popular
trials = 10_000
at_least_four = 0
trials.times do
array = [0] * days
ppl.times do
day = (rand() * days).truncate
array[day] += 1
end
at_least_four += 1 if array.max >= 4
end
puts "Probability of at least 4 shared birthdays is #{(at_least_four / trials.to_f) * 100}%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment