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

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
   
int getRandom(const int min, const int max) {
    return (rand() % (max - min + 1)) + min; 
}
  
int main(void) {
    srand(time(NULL));
    const int N = 7;
    const int X = 5;
   
    for (int i = 0; i < X; i++) {
        printf("Group %d: ", i + 1);
        for (int j = 0; j < N; j++) {
            printf("%d ", getRandom(1, 30));
        }
        printf("\n");
    }
   
    return 0;
}
 
   
/*
run:
   
Group 1: 7 26 19 29 7 21 19 
Group 2: 24 29 20 7 4 24 26 
Group 3: 19 20 5 2 5 13 28 
Group 4: 1 19 17 23 18 25 27 
Group 5: 4 15 20 10 10 30 30 
  
*/

 



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

Related questions

1 answer 111 views
1 answer 131 views
1 answer 135 views
1 answer 137 views
2 answers 136 views
...