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 N random numbers between min and max in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int getRandom(int min, int max) {
    return (rand() % (max - min + 1)) + min; 
}

int main(void) {
    srand(time(NULL));
    int N = 15;
 
    for (int i = 0; i < N; i++) {
        printf("%d ", getRandom(1, 30));
    }
 
    return 0;
}
 
 
 
/*
run:
 
run1:
12 27 1 13 28 11 9 3 14 4 19 13 6 28 30 

run2:
11 1 1 14 28 30 20 25 4 1 23 10 11 8 3

*/

 



answered Sep 23, 2024 by avibootz

Related questions

...