V149: Random Numbers According to Any Histogram

Author(s): F. James, K.S. Kölbig Library: MATHLIB
Submitter: Submitted: 20.03.1996
Language: Fortran Revised:

RNHRAN generates random numbers distributed according to any empirical (one-dimensional) distribution. The distribution is supplied in the form of a histogram. If the distribution is known in functional form, FUNLUX (V152) should be used instead.

Structure:

SUBROUTINE subprograms
User Entry Names: RNHRAN, RNHPRE
Files Referenced: Printer
External References: LOCATR, RANLUX

Usage:

CALL RNHPRE(Y,NBINS) (once for each histogram)
CALL RNHRAN(Y,NBINS,XLO,XWID,XRAN) (for each random number)

Y
Array of length NBINS at least containing the desired distribution as histogram bin contents on input to RNHPRE.
NBINS
Number of bins.
XLO
Lower edge of first bin.
XWID
Bin width.
XRAN
A random number returned by RNHRAN.

Method:

A uniform random number is generated using RANLUX (V115). The uniform number is then transformed to the user's distribution using the cumulative probability distribution constructed from his histogram. The cumulative distribution is inverted using a binary search for the nearest bin boundary and a linear interpolation within the bin. RNHRAN therefore generates a constant density within each bin.

Notes:

RNHPRE changes the values Y to form the cumulative distribution which is needed by RNHRAN. If Y already contains the cumulative distribution rather than the probability density, then RNHPRE should not be called, but in that case Y(NBINS) must be exactly equal to one. Numbers may be drawn from several different distributions in the same run by calling RNHRAN with different arrays Y1, Y2, etc. and (if desired) different values of NBINS, XLO, XWID (but always the same values for a given array Y). The routine RNHPRE should be used to initialize each array Yi.

The performance of the above method is nearly independent of the shape of the function or number of bins.

Error handling:

If the the input data to RNHPRE are not valid (some values negative or all values zero), an error message is printed, the input values are printed, and zero is returned instead of a random number. As many as five such messages may be printed to allow for possible errors in more than one distribution.

If RNHPRE is not called, and the input data are not already in cumulative form, RNHRAN performs the initialization itself and prints a warning message. RNHRAN recognizes that the data are not in cumulative form if tex2html_wrap_inline89 .
tex2html_wrap_inline91


Michel Goossens Wed Jun 5 08:57:20 METDST 1996