Last active
April 23, 2024 20:41
-
-
Save CezaryDanielNowak/aef0d55d0231295d746997b7f569e942 to your computer and use it in GitHub Desktop.
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
const generatePseudoRandom = (from, to) => parseInt(Math.random() * to) + from; | |
const ids = []; | |
let i = 0; | |
let lastCollision = 0; | |
while (i < 20) { // up to 20 collisions | |
const newId = generatePseudoRandom(1, 999999999); | |
if (ids.includes(newId)) { | |
console.error(`Collision detected after ${ids.length + 1 - lastCollision} IDs`); | |
lastCollision = ids.length + 1; | |
++i; | |
continue; | |
} | |
ids.push(newId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment