Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate a set with unique random numbers in a specified range using Node.js

1 Answer

0 votes
function generateUniqueRandomNumbersSet(total_numbers, max_range) {
    const st = new Set()
     
    while (st.size < total_numbers) {
        st.add(Math.floor(Math.random() * max_range) + 1)
    }
     
    return st
}
 
 
const st = generateUniqueRandomNumbersSet(15, 23);
 
console.log(st);
 
 
 
 
/*
run:
 
Set(15) { 23, 21, 20, 8, 15, 14, 11, 19, 4, 7, 2, 16, 5, 18, 12 }
 
*/

 



answered Mar 23, 2024 by avibootz
edited Mar 23, 2024 by avibootz
...