GENERATING RANDOM INTEGERS WITHIN A DESIRED RANGE

PARADOXES OF PROBABILITY USING THE C OR C++ rand() FUNCTION
index
Back to Deley's Homepage



GENERATING RANDOM INTEGERS WITHIN A DESIRED RANGE

All the random number generators examined here generate random values between [0,1), including 0 but not including 1 itself. The C and C++ rand() generator is the only exception to this. (See the next page for info on how to use the C and C++ rand() function).

For the following discussion [a,b) denotes the interval from a, including a, to b, not including b. The square bracket indicates that value is included. The parenthesis indicates that value is not included.

Here (r) is a real value returned from our random number generator. (r) is in the range [0,1).

(r) floating point value in range [0,1) = {r: 0 <= r < 1}

(x) = (r*M) = floating point value in range [0,M) = {x: 0 <= x < M}

(y) = (int)(x)
 = (int)(r*M)
 = integer in range [0,M)
 = {y: 0 < = y < M}
          1.  If M is an integer then the highest integer returned is M-1
then y = integer in the range [0,M-1]
i.e. (y) = {y: 0 <= y <= M-1}
2.If M is not an integer then the highest integer returned is (int)M
then y = integer in the range [0,(int)M]
and (y) = {y: 0<= y <= (int)M}

(z) = y+1
 = (int)(x) + 1
 = (int)(r*M) + 1
 = integer in the range [1,M+1)
 = {z: 1 <= z < M}
          1.  If M is an integer then the highest integer returned is M
then z = integer in the range [1,M] = {z: 1 <= z <= M}
2.If M is not an integer then the highest integer returned is (int)(M+1)
then z = integer in the range [1,(int)(M+1)]
i.e. (z) = {z: 1<= z <= (int)(M+1)}

The range of our random generator can be expanded or contracted by choosing M. It can be shifted up or down the number line by adding or subtracting a fixed value. It can even be flipped over by multiplying by -1.




PARADOXES OF PROBABILITY USING THE C OR C++ rand() FUNCTION
index
Back to Deley's Homepage