How to generate 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(time(NULL));
    const int N = 15;
 
    for (int i = 0; i < N; i++) {
        std::cout << getRandom(1, 30) << " ";
    }
}
 
 
/*
run:
   
run1:
10 30 19 5 7 24 1 1 2 15 11 9 13 30 13 
  
run2:
9 16 14 29 17 20 15 21 4 23 14 15 6 22 16 
  
*/

 



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

Related questions

2 answers 142 views
1 answer 118 views
1 answer 118 views
1 answer 140 views
...