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

51,901 answers

573 users

How to generate random numbers in a specific range in C

1 Answer

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

int RandXY(int min, int max);

int main(void)
{
    int i, n;
  
    for (i = 0; i < 13; i++)
    {
        n = RandXY(3, 7);
        printf ("%d\n", n);
    }
  
    return 0;
}

int RandXY(int min, int max)
{
    static int ones = 0;
    int rn;
  
    if (ones == 0)
    {
        srand(time(NULL));
        ones = 1;
    }
 
    rn = rand() % (max - min + 1) + min;
  
    return rn;
}

/*

run:

4
5
4
6
6
7
6
6
6
5
4
4
3

*/




answered Sep 11, 2014 by avibootz
edited May 22, 2015 by avibootz
...