Skip to content

Instantly share code, notes, and snippets.

@ivan-developer-01
Last active November 20, 2023 14:37
Show Gist options
  • Save ivan-developer-01/f7ecd07350075ed56a2aaf5baaafb4c3 to your computer and use it in GitHub Desktop.
Save ivan-developer-01/f7ecd07350075ed56a2aaf5baaafb4c3 to your computer and use it in GitHub Desktop.
Check the real randomness of Math.random()
function checkRandomness(num) {
// 1024 random nums from 0 to num
const rand = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
.split("").map(a => ~~(Math.random() * num));
for (let i = 0; i < rand.length; i++) {
// If the total count of the number with value of rand[i] is 1, then we continue
if (rand.filter(a => a === rand[i]).length === 1) continue;
// Otherwise we inform user about that
console.log("The collided numbers was [" + rand.filter(a => a === rand[i]).join(", ") + "]");
return true;
}
// If still no colliding numbers was found, we just
return false;
}
console.log(checkRandomness(1024)); // Very many collisions
console.log(checkRandomness(2048)); // Still many collisions
console.log(checkRandomness(4096)); // A few collisions (still bad)
console.log(checkRandomness(8192)); // A few collisions
console.log(checkRandomness(10000)); // Still a few collisions!
console.log(checkRandomness(100000)); // Still a few collisions! (How? I was a little surprised to see the result)
console.log(checkRandomness(1000000)); // Rare collisions, yet better than the previous collisions (i.e. collisions are rare, but they are there!)
console.log(checkRandomness(10000000)); // Rare collisions! A bit less than in previous example yet that's bad.
console.log(checkRandomness(100000000)); // STILL, I was able to find a LOT of collisions out of 10,000 arrays with 1024 random nums.
// The examples show how unsafe Math.random is.
// Conclusions: don't use Math.random() if the app is doing some pretty important
// things like creating UUIDs, generating BTC-like wallets etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment