Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

function

random

I would like to make random numbers in a specific range, like "pick a random number between 18 and 35"? How can I do that with the rand() function?

like image 803
Mr.Work Avatar asked Nov 24 '10 18:11

Mr.Work


1 Answers

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().

like image 164
NealB Avatar answered Oct 04 '22 03:10

NealB