next up previous index
Next: V151 Random Numbers Up: CERNLIB Previous: V138 Multinomial Random

V150 Random Numbers According to Any Histogram

Routine ID: V150
Author(s): F. JamesLibrary: MATHLIB
Submitter: Submitted: 15.09.1978
Language: FortranRevised:

HISRAN 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, FUNRAN (V151) should be used instead.

Structure:

SUBROUTINE subprograms
User Entry Names: HISRAN, HISPRE
Files Referenced: Printer
External References: LOCATF (E106), RNDM (V104)

Usage:

CALL HISPRE(Y,NBINS)(once for each histogram)
CALL HISRAN(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 HISPRE.
NBINS
Number of bins.
XLO
Lower edge of first bin.
XWID
Bin width.
XRAN
A random number returned by HISRAN.

Method:

A uniform random number is generated using RNDM (V104). (The user may therefore use RDMOUT and RDMIN (V104) to restart a run.) 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. HISRAN therefore generates a constant density within each bin.

Notes:

HISPRE changes the values Y to form the cumulative distribution which is needed by HISRAN. If Y already contains the cumulative distribution rather than the probability density, then HISPRE 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 HISRAN 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 HISPRE 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 HISPRE 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 HISPRE is not called, and the input data are not already in cumulative form, HISRAN performs the initialization itself and prints a warning message. HISRAN recognizes that the data are not in cumulative form if Y(NBINS) ≠1 .

V151



next up previous index
Next: V151 Random Numbers Up: CERNLIB Previous: V138 Multinomial Random


Janne Saarela
Mon Apr 3 15:06:23 METDST 1995