How to use the rand function to make numbers in a specific range?

Depending on the language you are using, the built in Random number generator may already have this capability - do a bit more research.

Suppose that the random number generator that you have always returns numbers in some given range. Just for the sake of argument, lets say the range is 0..65536 but you want random numbers in the range Low..High, 18..35 in your example.

The wrong way to do it would be something like:

 r = (rand() % (High - Low + 1)) + Low

rand() returns a number in range 0..65536. Take the remainder after dividing by (High - Low + 1) which in this example is (35 - 18 + 1 = 18). The result is a number between 0..17. To this you add Low (18) which shifts the result, r, into the range 18..35. The range you are looking for.

Numbers generated this way do not have a uniform distribution in cases where the divisor used to obtain the remainder is not an even multiple of the upper limit returned by the rand() function. See the Fischer Yates Algorithm - Modulo Bias. To remove this bias you need to calculate the largest number that is smaller than what rand() returns but evenly divides by (High - Low + 1). In your case that is 3640 * 18 = 65520. Use this as a high range filter on the numbers returned by rand() as follows:

  do forever {
     r = rand()
     if r <= 65520 then {
         r = (r % (High - Low + 1)) + Low
         break
         }
     } 

Now the random numbers you generate should have the same distribution characteristics as rand().


If this is written in C, then you are pretty close. Compiling this code:

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

int main() {
    int i;
    for (i = 0; i < 1000000; i++) {
        printf("%d\n", rand()%(35-18+1)+18);
    }
}

And running it in a pipeline produces this output:

chris@zack:~$ gcc -o test test.c
chris@zack:~$ ./test | sort | uniq -c
  55470 18
  55334 19
  55663 20
  55463 21
  55818 22
  55564 23
  55322 24
  55886 25
  55947 26
  55554 27
  55342 28
  55526 29
  55719 30
  55435 31
  55669 32
  55818 33
  55205 34
  55265 35

The key is you forgot to add 1 -- the fencepost error.

You can generalize this into a function:

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