vendredi 21 avril 2023

Javascript math.random gives too many duplicate values

Using NodeJS 14. I have a script that needs to generate 10,000 unique numbers between 1 and 10000. i have a function that takes 1 as min and 10000 as max. When I run it, only ~7000 are unique the rest are duplicates. I understand that not all 10k would be unique, but 30%? What can I do to get more of then unique?

function is:

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

sorted sample 1856 1871 1873 1879 1879 1885 1895 1903 1911 1911




Aucun commentaire:

Enregistrer un commentaire