public
Last active

The Birthday Problem!

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.