Skip to content

Instantly share code, notes, and snippets.

@benders
benders / birthday.rb
Created May 19, 2009 17:25
Estimate the collision rate of k keys in space n
# Approximates the chance of at least one collision in a random
# distribution of k items across n possible IDs. Adapted from
# Karsten's approximate calculator at http://tinyurl.com/5zt6ol
def collision_probability( k, n )
1 - Math.exp((-k ** 2) / ( 2.0 * n).to_f)
end
# The odds of two people having the same birthday are slightly
# greater than 50% if you have 23 or more people.
puts collision_probability( 23, 365 ) # => 0.515509538061517