Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Last active April 23, 2024 20:41
Show Gist options
  • Save CezaryDanielNowak/aef0d55d0231295d746997b7f569e942 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/aef0d55d0231295d746997b7f569e942 to your computer and use it in GitHub Desktop.
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