-3

I'm learning about the rand() function in C, as I want to use it to generate a random number in a range. However, I have a question about a part of the algorithm below.

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

int main()
{
    const MAX = 20, MIN = 1;
    srand(time(NULL));
    int randNumber = rand() % (MAX - MIN + 1) + MIN;
    printf("%d", randNumber);
    // yeu cau nhap so
    int duDoan;
    printf("Moi ban du doan con so:");
    scanf("%d", &duDoan);
    // chay vong lap kiem tra
    while(duDoan != randNumber) {
        printf("Ban da sai. Moi nhap lai:");
        scanf("%d", &duDoan);
    }
    printf("Ban da nhap dung. Dap an la: %d ", randNumber);

    return 0;
}

What confuses me here is why we have to add + MIN in this line:

rand() % (MAX - MIN + 1) + MIN;

If I leave it, what will the result be?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Quốc Cường
  • 417
  • 4
  • 12
  • Surely you can run the code and experiment with it. If rand() % 50 gives you a number between 0 and 49, what would you do if you wanted a number between 30 and 79? – Retired Ninja Apr 28 '19 at 18:14
  • The subtraction tells you how many numbers are needed in the range 0.. and when you add the random number to `MIN` then *voilà* you have a number in the desired range. – Weather Vane Apr 28 '19 at 18:18
  • What does `rand ()% (MAX - MIN + 1)` deliver, and what do you have to do with it to get a number inclusive to the range `[MIN...MAX]` ? – WhozCraig Apr 28 '19 at 18:20
  • 1
    As a side note, Stack Overflow is an English site. Translating the variable names and strings in the code would be appreciated. – S.S. Anne Apr 28 '19 at 18:20
  • https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c – OznOg Apr 28 '19 at 18:42
  • Note this has little to do with the behaviour of `rand()` and everything to do with the semantics of the `%` operator. – Clifford Apr 28 '19 at 19:30
  • 1
    @JL2210 : I think we can manage. Asking for a rewrite of the code in question is somewhat unreasonable and will possibly introduce errors not in the original code or relevant to the question. I'd rather the code posted was the exact code the question referred to. In this case the part of the code in question _is_ in English in any case. – Clifford Apr 28 '19 at 19:35
  • OK. If anybody's curious as to what language this is (so they can translate it and understand the code better), it's Vietnamese. – S.S. Anne Apr 28 '19 at 19:59
  • Sorry everyone this is my first time in this forum. I hope everyone will give up on the language problem, I will fix it next time – Quốc Cường Apr 29 '19 at 05:41

1 Answers1

2

rand() is a number between 0 and RAND_MAX.

rand() % n is a number between 0 and n - 1. If you want a value from 0 to n, then you need rand() % (n+1).

In your example (MAX - MIN + 1) is the span of integer values to generate, while MIN is the lower value. So for example where:

MIN = -10
MAX = 10

the span n :

n = (MAX - MIN + 1) = 21

so that:

rand() % n

yields values from 0 to 20, and

rand() % n - MIN

is -10 to +10. Without the +1, it would incorrectly be -10 to +9.

Note that where a statistically high quality random number is required restricting the span by the use of % is flawed and will introduce a bias when n is not a factor of RAND_MAX + 1. In that case (int)(n * ((double)rand() / (double)RAND_MAX)) is a better solution, so you would have:

int randNumber = (int)((MAX - MIN) * ((double)rand() /
                                      (double)RAND_MAX)) + MIN ;

Note there is no +1 here because the range of (double)rand() / (double)RAND_MAX is 0 to 1, so multiplying by n gives 0 to n inclusive.

Clifford
  • 88,407
  • 13
  • 85
  • 165