This file contains hidden or 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
# 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 |
NewerOlder