Skip to content

Instantly share code, notes, and snippets.

@ItsShadowl
Forked from samcorcos/collision.js
Created June 20, 2018 09:22
Show Gist options
  • Save ItsShadowl/4738b14501eb55d0be645c89f8edfa6e to your computer and use it in GitHub Desktop.
Save ItsShadowl/4738b14501eb55d0be645c89f8edfa6e to your computer and use it in GitHub Desktop.
Algorithm for calculating hash collision probability
const calculate = (n, k) => {
const exponent = (-k * (k - 1)) / (2 * n)
return 1 - Math.E ** exponent
}
// where `n` is the number of possible unique hashes
// where `k` is the number of values created
// calculate(100000, 25) => 0.0029955044966269995 aka 0.29% chance of collision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment