Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/9423852 to your computer and use it in GitHub Desktop.
Save havenwood/9423852 to your computer and use it in GitHub Desktop.
Birthday Paradox
class Bdays
attr_reader :birthdates
def initialize n
@birthdates = Array.new(n) { [*0..365].sample }.map do |s|
birthday = Time.new(0) + s * 60 * 60 * 24
[birthday.month, birthday.day]
end
shared_birthdates
end
def shared_birthdates
@shared ||= @birthdates.each_with_object(Hash.new 0) do |date, result|
result[date] += 1 if @birthdates.count(date) > 1
end
end
end
bdays = Bdays.new 50
bdays.shared_birthdates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment