Generating Uniform Random Variables

Definition:

A uniform random variable has a continuous distribution, and it can take on any real value within some specified interval bounded by the two endpoints A and B.  In addition, the selection of each value is completely unbiased, so that each possible outcome is just as likely to occur as every other outcome.

By convention, we usually assume that the lower bound, A, is included in the range, but the upper bound, B, is not. However, since there are so many different possibilities available (i.e., even if we approximate the uncountably infinite number of real numbers in the interval by a finite set of floating point numbers, there are still about 2**32 different values to choose from), it is highly unlikely to generate any specific value (such as A or B), so in practice this distinction isn't really important.

How to Generate:

  1. The first step is to generate a uniform random number, say U, whose value is between 0 and 1. The easiest method is to use the Linux rand() function to generate a random 32-bit integer and divide by the maximum value, RAND_MAX, to convert it into a real number between 0 and 1, like this:
    1.  
      U = rand()/(RAND_MAX+1.0);
       
  2. The next step is to apply a conversion formula to change it from a uniform (0,1) random variable to a uniform (A,B) random variable, say X, like this:
    1.  
      X = A + U * (B - A);
       
  3. Each additional call to the rand() function will return a different value, so repeating the above two steps will generate a sequence of uniform (A,B)  random variables. However, note that if you rerun your program a second time, the rand() function will generate the same sequence of "random" numbers again! Thus, in order to conduct a second "experiment" rather than exactly repeating the first one exactly, you must call the srand() function (once only!!!!) to change the hidden random number "seed" that controls the sequence.