DEV Community

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

Collapse
 
shane325 profile image
Shane Barry • Edited

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 • Edited

This is so cool! Gotta love new JS features

Collapse
 
k16e profile image
Kabolobari Benakole

How's this solution work, though. And it's rather difficult to read. Would you break it down? I mean it looks handy...but