Created
January 7, 2009 05:39
-
-
Save listrophy/44189 to your computer and use it in GitHub Desktop.
The Birthday Problem!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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