Last active
August 24, 2018 11:32
-
-
Save Kasahs/eb9282e993893129659a53a3aef50918 to your computer and use it in GitHub Desktop.
Test your PRNG for collision frequency
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
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