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,870 questions

51,793 answers

573 users

How to generate a random integer in a range (min...max) in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int get_random_in_range(int min, int max) {
    return min + rand() % (max - min + 1);
}

int main() {
    // Seed the random number generator
    srand(time(0));

    int min = 3, max = 14;

    for (int i = 0; i < 20; i++) {
        int randomNumber = get_random_in_range(min, max);
        printf("%d\n", randomNumber);
    }

    return 0;
}


  
/*
run:
  
4
11
11
14
7
3
13
9
6
7
9
3
13
7
8
11
3
8
10
12
  
*/

 



answered Nov 3, 2024 by avibootz

Related questions

4 answers 563 views
1 answer 124 views
2 answers 179 views
4 answers 1,645 views
1 answer 106 views
1 answer 109 views
...