RNGDT
Generates pseudorandom numbers from a general discrete distribution using a table lookup method.
Required Arguments
IMIN — Smallest value the random deviate can assume. (Input)
This is the value corresponding to the probability in CUMPR(1).
NMASS — Number of mass points in the discrete distribution. (Input)
CUMPR — Vector of length at least
NMASS + 1 containing in the first
NMASS positions the cumulative probabilities and, possibly, indexes to speed access to the probabilities. (Input)
IMSL routine
RNGDS can be used to initialize
CUMPR properly. If no elements of
CUMPR are used as indexes,
CUMPR(
NMASS + 1) is 0.0 on input. The value in
CUMPR(1) is the probability of
IMIN. The value in
CUMPR(
NMASS) must be exactly 1.0 (since this is the
CDF at the upper range of the distribution).
IR — Vector of length NR containing the random discrete deviates. (Output)
Optional Arguments
NR — Number of random numbers to generate. (Input)
Default: NR = size (IR,1).
FORTRAN 90 Interface
Generic: CALL RNGDT (IMIN, NMASS, CUMPR, IR [, …])
Specific: The specific interface names are S_RNGDT and D_RNGDT.
FORTRAN 77 Interface
Single: CALL RNGDT (NR, IMIN, NMASS, CUMPR, IR)
Double: The double precision name is DRNGDT.
Description
Routine
RNGDT generates pseudorandom deviates from a discrete distribution, using the table
CUMPR, which contains the cumulative probabilities of the distribution and, possibly, indexes to speed the search of the table. The routine
RNGDS can be used to set up the table
CUMPR.
RNGDT uses the inverse CDF method to generate the variates.
Comments
1. Informational error
Type | Code | Description |
---|
3 | 1 | The value in CUMPR(NMASS) is not exactly 1.0, but it was considered close enough to 1.0 that is was set to that value. |
2. In the interest of efficiency, this routine does only limited error checking. If
CUMPR is generated by the routine
RNGDS, the error checking is sufficient.
3. The routine
RNSET can be used to initialize the seed of the random number generator. The routine
RNOPT can be used to select the form of the generator.
Examples
Example 1
These examples are the same ones used for the routine
RNGDS. In this first example,
RNGDS is used to set up a table and then
RNGDT is used to generate five pseudorandom variates from the discrete distribution:
Pr(X = 1) = .05
Pr(X = 2) = .45
Pr(X = 3) = .31
Pr(X = 4) = .04
Pr(X = 5) = .15
The cumulative probabilities are input directly in CUMPR, and three indexes are computed by RNGDS (NNDX = 4). Since the number of mass points is so small, the indexes would not have much effect on the speed of the generation of the random variates.
USE UMACH_INT
USE RNGDS_INT
USE RNSET_INT
USE RNGDT_INT
IMPLICIT NONE
INTEGER LCUMPR, NR
PARAMETER (LCUMPR=9, NR=5)
!
INTEGER IMIN, IOPT, IR(NR), ISEED, NMASS, NNDX, NOUT
REAL CUMPR(LCUMPR), DEL, PRF
EXTERNAL PRF
!
CALL UMACH (2, NOUT)
IMIN = 1
NMASS = 5
CUMPR(1) = 0.05
CUMPR(2) = 0.50
CUMPR(3) = 0.81
CUMPR(4) = 0.85
CUMPR(5) = 1.00
IOPT = 1
NNDX = 4
DEL = 0.00001
! Set up table
CALL RNGDS (PRF, DEL, NNDX, IMIN, NMASS, CUMPR, IOPT=IOPT)
ISEED = 123457
CALL RNSET (ISEED)
! Generate variates
CALL RNGDT (IMIN, NMASS, CUMPR, IR)
WRITE (NOUT,99999) IR
99999 FORMAT (' Discrete random deviates: ', 5I4)
END
!
! Dummy function
REAL FUNCTION PRF (IX)
INTEGER IX
!
PRF = 0.0
RETURN
END
Output
Discrete random deviates: 5 2 3 3 4
Example 2
In this example,
RNGDS is used to set up a table and then
RNGDT is used to generate five pseudorandom variates from the binomial distribution with parameters 20 and 0.5. The routine
BINPR (see
Chapter 17, “Probability Distribution Functions and Inverses”) is used to compute the probabilities.
USE UMACH_INT
USE RNGDS_INT
USE RNSET_INT
USE RNGDT_INT
IMPLICIT NONE
INTEGER LCUMPR, NR
PARAMETER (LCUMPR=33, NR=5)
!
INTEGER IMIN, IR(NR), ISEED, NMASS, NNDX, NOUT
REAL CUMPR(LCUMPR), DEL, PRF
EXTERNAL PRF
!
CALL UMACH (2, NOUT)
IMIN = 0
NMASS = 21
NNDX = 12
DEL = 0.00001
! Set up table
CALL RNGDS (PRF, DEL, NNDX, IMIN, NMASS, CUMPR)
ISEED = 123457
CALL RNSET (ISEED)
! Generate variates
CALL RNGDT (IMIN, NMASS, CUMPR, IR)
WRITE (NOUT,99999) IR
99999 FORMAT (' Binomial (20, 0.5) random deviates: ', 5I4)
END
!
! Compute binomial probabilities
REAL FUNCTION PRF (IX)
USE BINPR_INT
INTEGER IX
!
PRF = BINPR(IX,20,0.5)
RETURN
END
Output
Binomial (20, 0.5) random deviates: 14 9 12 10 12
Published date: 03/19/2020
Last modified date: 03/19/2020