Skip to content

Instantly share code, notes, and snippets.

@listrophy
Created January 7, 2009 05:39
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 listrophy/44189 to your computer and use it in GitHub Desktop.
Save listrophy/44189 to your computer and use it in GitHub Desktop.
The Birthday Problem!
lower_bound = (ARGV.shift || 2).to_i
upper_bound = (ARGV.shift || 35).to_i
iterations = (ARGV.shift || 20_000).to_i
(lower_bound..upper_bound).each do |person_count|
matches = 0
iterations.times do
matches += 1 if (1..person_count).map{|p| rand(365)}.uniq!
end
puts "Person count: %3d Success: %3.1f" %
[person_count, matches * 100.0 / iterations]
end
__END__
$ ruby birthday.rb 22 24
Person count: 22 Success: 47.6
Person count: 23 Success: 50.6
Person count: 24 Success: 54.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment