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
2
Answers
To obtain exactly 10000 unique random numbers, you can just shuffle all the integers from 1 to 10000.
You can loop through the size of the set an keep pushing into it until the size if your desired size. Set guarantees uniquness.