DEV Community

Discussion on: Generate unique (non-repeating) random numbers

Collapse
shane325 profile image
Shane Barry • Edited on

Nice. Here's another implementation using a Set()

const randomUnique = (range, count) => {
    let nums = new Set();
    while (nums.size < count) {
        nums.add(Math.floor(Math.random() * (range - 1 + 1) + 1));
    }
    return [...nums];
}
Enter fullscreen mode Exit fullscreen mode
Collapse
sagdish profile image
Sagdi Formanov Author • Edited on

This is so cool! Gotta love new JS features