dimanche 10 octobre 2021

Generate random number that doesn't previously exist in a list

How can I create a random 16 digit number that doesn't previously exist in a list?

Let's say I have the following list:

const nums = [7856328870763265, 0107654389657487];

I need a function to generate random 16 digit numbers but that doesn't create duplicates.

I have the following:

const val = Math.floor(1000 + Math.random() * 9000).toString();
const random16digitnumber = +val+val+val+val;
console.log(random16digitnumber); //4279427942794279

But im afraid at some point it will start creating duplicates. I'm using the random16digitnumber as a primary key in a SQL database for a small school project.

So going back to my example, how can I create a random 16 digit number that doesn't previously exist in an array?




Aucun commentaire:

Enregistrer un commentaire