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 124 views
2 answers 127 views
1 answer 100 views
1 answer 107 views
...