Skip to content

Instantly share code, notes, and snippets.

@Kasahs
Last active August 24, 2018 11:32
Show Gist options
  • Save Kasahs/eb9282e993893129659a53a3aef50918 to your computer and use it in GitHub Desktop.
Save Kasahs/eb9282e993893129659a53a3aef50918 to your computer and use it in GitHub Desktop.
Test your PRNG for collision frequency
var num = (len, rng) => Math.floor(10 ** (len - 1) + rng() * 9 * 10 ** (len - 1));
var test = (rng, times) => {
var map = {}
for(let i=0; i<times; i++){
let rn = rng()
map[rn] = map[rn] ? map[rn] + 1 : 1
}
let collisions = Object.values(map).filter(val => val > 1)
let total = collisions.reduce((a,b) => a + b, 0) - collisions.length
console.log(total)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment