1

I've tried but it doesn't seem to give me the desired numbers. Here's my code so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n, i;
   printf("Value of n:\n");
   scanf("%d", &n);
   int t[n];
   for(i=0; i<n; i++)
    printf("%d ", rand()%20+30);

return 0;
}
Dora Natalia
  • 31
  • 1
  • 8
  • Welcome to Stack Overflow. This is most definitely a duplicate question — one that has been asked and answered many times before. The range may vary, but values between 10 and 20 is not very different from between 20 and 30. – Jonathan Leffler Apr 27 '17 at 16:58
  • @JonathanLeffler I found a more elaborate dupe, with a step, you're right. – Jean-François Fabre Apr 27 '17 at 17:01

4 Answers4

4
printf("%d ", rand()%20+30);

generates random numbers between 30 and 50 (not included). You need

printf("%d ", rand()%10+20);

to generate between 20 and 30 (not included)

10 being the range, and 20 being the offset.

To include both end points (giving 11 possible values):

printf("%d ", rand()%11+20);
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I'm going to argue that there's an off-by-one bug here too; the range quoted normally includes both end points in plain English, so it should be `rand() % (max - min + 1) + min`. That ignores subtleties like the slight bias when `(max - min + 1)` does not exactly divide RAND_MAX. – Jonathan Leffler Apr 27 '17 at 17:13
  • edited to enclude both end points (that was clear enough in my previous answer). You're right about the bias. I wouldn't vouch about the uniform properties of that code. – Jean-François Fabre Apr 27 '17 at 17:16
1

these are the expressions

number = 21 + rand() % 9 // for (20, 30)

number = 20 + rand() % 11 // for [20, 30]

Rajeev Singh
  • 3,292
  • 2
  • 19
  • 30
  • Apart from the off-by-one bug... – Jonathan Leffler Apr 27 '17 at 17:03
  • @JonathanLeffler it is ok now I think! – Rajeev Singh Apr 27 '17 at 17:07
  • Given the edit to `number = 21 + rand() % 9`, the result looks like a number in the range 21..29 now. The start point was OK; it was the modulus that was giving trouble. Assuming max > min, then `rand() % (max - min + 1) + min` is normal. – Jonathan Leffler Apr 27 '17 at 17:09
  • the question says to generate numbers between 20 & 30, so I think they should not be included. – Rajeev Singh Apr 27 '17 at 17:15
  • I'll repeat a comment I made to someone else:— Weird. If someone says "give me a number in the range 1 to 6" for simulating a six-sided dice, the end values are both included. Normal speech includes the end points. If you're going to use the asymmetric range (or the exclusive range), say so. But common speech includes the end points. – Jonathan Leffler Apr 27 '17 at 17:16
  • Eddited my answer!! I agree to your point, but there is difference in saying 'a number in range 20-30' and 'a number between 20-30'. – Rajeev Singh Apr 27 '17 at 17:22
0

Try rand()%10+20.

rand()%10 gives numbers between 0 and 9. Adding then 20 will give numbers between 20and 29.

For numbers between 20 and 30 (inclusive), use rand()%11+20.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

To generate random number in range [A .. B], first take difference between numbers: D = B-A, then generate a random number in the range of [0..D] and add that to A:

unsigned myRand(unsigned fromA, unsigned toB)
{
    if (fromA > toB)  // check if fromA is 
        return myRand(toB, fromA);
    int d = toB - fromA; // difference between numbers: D = B-A  
    int r = rand() % (d+1); // random number in the range of [0..D]
    return r + fromA;
}

Or, you may just to it manually like this:

20 + (rand()%11)

Also, note that depending on implementation rand() returns values between 0 and RAND_MAX, so you won't be able to create random values large than RAND_MAX (which might be as small as 32767). In this cases you may combine multiple rand() calls to get large random numbers:

unsigned r = (rand() << 16) | rand();
Pavel P
  • 15,789
  • 11
  • 79
  • 128