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,872 questions

51,796 answers

573 users

How to generate X groups of N random numbers between min and max in C++

1 Answer

0 votes
#include <iostream>
#include <ctime>
  
int getRandom(const int min, const int max) {
    return (rand() % (max - min + 1)) + min; 
}
  
int main() {
    srand(static_cast<unsigned int>(time(nullptr)));
    const int N = 7;
    const int X = 5;
  
    for (int i = 0; i < X; i++) {
        std::cout << "Group " << (i + 1) << ": ";
        for (int j = 0; j < N; j++) {
            std::cout << getRandom(1, 30) << " ";
        }
        std::cout << std::endl;
    }
}
  
  
/*
run:
    
Group 1: 18 6 27 19 17 13 1 
Group 2: 1 12 22 1 16 14 26 
Group 3: 24 2 11 12 1 3 26 
Group 4: 17 11 28 23 4 20 4 
Group 5: 8 30 30 18 27 26 6 
   
*/

 



answered Sep 24, 2024 by avibootz
edited Sep 24, 2024 by avibootz

Related questions

1 answer 106 views
2 answers 112 views
1 answer 95 views
1 answer 95 views
...