Generating a continuous numerical value

We have already shown how to generate a random rectangularly distributed value, but how can a numerical value be generated from another continuous distribution? There are several algorithms to generate random values from continuous numerical distributions and many are more efficient than the one that we describe below. However the following method is relatively easy to describe and understand.

Consider the diagram below which encloses the distribution's probability density function with a rectangle.

A random value from the distribution can be generated by repeatedly generating a random point within the rectangle, until the point lies within the shaded area of probability density function. The x-coordinate of this point will be a random value from the required distribution.

  1. Generate a random proportion between 0 and 1 from a rectangular distribution. A random horizontal position within the enclosing rectangle, x, is this proportion of the way between the left and right edges of the rectangle.
  2. In a similar way, generate a random vertical position, d, within the enclosing rectangle.
  3. If the point lies within the target probability density, return the value x as a value from this distribution. Otherwise, repeat from step 1.

The diagram below illustrates for a distribution of values between 0 and 1 (actually a normal distribution) to avoid the complication of scaling the x-values.

Click Generate value to generate a random horizontal and vertical position within the bounding rectangle (0 to 1 horizontally and vertically).

If this point lies under the probability density function, the horizontal position is accepted as a value from the distribution. Otherwise, the value is rejected and another random point must be generated.

Click Generate value several times until 30 or more values have been generated. The distribution of the generated values (the horizontal positions of the crosses under the probability density function) should conform reasonably with the target distribution!