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
VALUES_PER_SECOND = 10_000_000.0 | |
# Probability of collision of a single generation | |
# - gen_per_second = how many values are generated per second | |
def p_gen(machine_count, gen_per_second) | |
value_count = VALUES_PER_SECOND / gen_per_second | |
1 - Math.exp(-machine_count*(machine_count-1)/(2*value_count)) | |
end | |
# Probability of collision in a given interval of time | |
def p_time(machine_count, gen_per_second, time_in_seconds) | |
p_single = p_gen(machine_count, gen_per_second) | |
repeat_count = gen_per_second * time_in_seconds | |
1 - (1 - p_single)**repeat_count | |
end | |
puts "Probability of 2 machines creating a collision in a certain generation, generating 1000 timestamps per second:" | |
puts p_gen(2, 1000) | |
puts "Probability of 2 machines creating a collision, generating 1000 timestamps per second, in 1 minute:" | |
puts p_time(2, 1000, 60) | |
puts "Probability of 2 machines creating a collision, generating 1 timestamp per second, in 90 days:" | |
puts p_time(2, 1, 3600*24*90) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment