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 <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 106 views
1 answer 110 views
1 answer 113 views
2 answers 112 views
...