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

51,826 answers

573 users

How to generate random numbers between two numbers in C

3 Answers

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

int main(void)
{
    int r;
    
    srand((unsigned)time(NULL));
    
    for(int i = 0; i < 100; i++)
    {
        // rand() % (10) = between 0 - 9 + 1 = between 1 - 10
        r = rand() % 10 + 1; // between 1 - 10 
        printf("%d\t", r);
    }
    return 0;
}

  
/*
  
run:
  
6       3       10      10      9       8       4       6       6       5
6       3       9       3       4       10      10      1       6       4
1       6       1       7       1       3       6       6       3       6
4       10      6       1       2       7       1       3       9       5
2       5       6       4       4       3       3       3       6       10
10      3       6       10      5       4       9       5       1       1
5       5       8       10      10      1       6       7       7       1
9       2       5       2       5       9       9       9       8       1
1       7       9       7       2       10      10      1       10      4
4       7       3       9       9       5       10      1       6       2

*/

 



answered Nov 6, 2015 by avibootz
edited Nov 6, 2015 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

int main(void)
{
    int r;
    
    srand((unsigned)time(NULL));
    
    for(int i = 0; i < 100; i++)
    {
        // rand() % (11) = between 0 - 10 + 3 = between 3 - 13
        r = rand() % (13 - 3 + 1) + 3; // between 3 - 13 
        printf("%d\t", r);
    }
    return 0;
}

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

*/

 



answered Nov 6, 2015 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

int main(void)
{
    int r, a = 10, b = 20;
    
    srand((unsigned)time(NULL));
    
    for(int i = 0; i < 100; i++)
    {
        // rand() % (21) = between 0 - 20 + 10 = between 10 - 20
        r = rand() % (b - a + 1) + a; // between 10 - 20
        printf("%d\t", r);
    }
    return 0;
}

  
/*
  
run:
  
16      17      14      16      15      12      19      14      13      10
19      13      17      15      12      16      15      12      17      20
16      16      13      20      12      11      12      15      10      13
13      12      15      10      14      16      13      11      17      17
14      20      19      18      13      12      17      17      16      11
11      12      11      17      20      17      11      13      17      18
17      12      18      13      17      20      20      15      11      13
11      18      17      10      13      16      18      16      13      16
15      14      14      12      17      20      13      12      20      19
13      12      19      12      10      14      19      18      13      17

*/

 



answered Nov 6, 2015 by avibootz

Related questions

1 answer 188 views
1 answer 152 views
1 answer 106 views
1 answer 110 views
1 answer 106 views
1 answer 114 views
...