RAND_GEN_DISCR Function
Generates pseudorandom numbers from a general discrete distribution using an alias method or optionally a table lookup method.
Usage
result = RAND_GEN_DISCR(n, imin, nmass, probs)
Input Parameters
n—Number of random numbers to generate.
imin—Smallest value the random deviate can assume. This is the value corresponding to the probability in probs(0).
nmass—Number of mass points in the discrete distribution.
probs—Array of length nmass containing probabilities associated with the individual mass points. The elements of probs must be nonnegative and must sum to 1.0.
If the keyword Table is used, then probs is a 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. DISCR_TABLE Function can be used to initialize probs properly. If no elements of probs are used as indexes, probs (nmass) is 0.0 on input. The value in probs(0) is the probability of imin. The value in probs (nmass – 1) must be exactly 1.0 (since this is the CDF at the upper range of the distribution.)
Returned Value
result—Integer array of length n containing the random discrete deviates.
Input Keywords
DoubleIf present and nonzero, double precision is used.
Table—If present and nonzero, generate pseudorandom numbers from a general discrete distribution using a table lookup method. If this keyword is used, then probs is a 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. DISCR_TABLE Function can be used to initialize probs properly.
Discussion
RAND_GEN_DISCR generates pseudorandom numbers from a discrete distribution with probability function given in the vector probs; that is:
Pr(X = i) = pj
for i = imin, imin + 1, ..., imin + nm1
where:
j = i – imin + 1, pj = probs(j), imin = imin, and nm = nmass
The algorithm is the alias method, due to Walker (1974), with modifications suggested by Kronmal and Peterson (1979).
If the keyword Table is used, RAND_GEN_DISCR generates pseudorandom deviates from a discrete distribution, using the table probs, which contains the cumulative probabilities of the distribution and, possibly, indexes to speed the search of the table. DISCR_TABLE Function can be used to set up the table probs. RAND_GEN_DISCR uses the inverse CDF method to generate the variates.
Example 1
In this example, RAND_GEN_DISCR is used to generate five pseudorandom variates from the discrete distribution:
Pr(X = 1) = 0.05
Pr(X = 2) = 0.45
Pr(X = 3) = 0.31
Pr(X = 4) = 0.04
Pr(X = 5) = 0.15
probs = [0.05, 0.45, 0.31, 0.04, 0.15]
n = 5
imin = 1
nmass = 5
RANDOMOPT, Set_seed = 123457
r = RAND_GEN_DISCR(n, imin, nmass, probs)
PM, r ; PV-WAVE prints the following:
; 3
; 2
; 2
; 3
; 5
Example 2
In this example, the DISCR_TABLE Function is used to set up a table and then RAND_GEN_DISCR is used to generate five pseudorandom variates from the binomial distribution with parameters 20 and 0.5.
FUNCTION PRF, ix
   RETURN,  BINOMIALPDF(ix, 20, .5)
END
imin = 0
nmass = 21
RANDOMOPT, Set_seed = 123457
cumpr = DISCR_TABLE('prf', 0.00001, 12, imin, nmass)
r = RAND_GEN_DISCR(n, imin, nmass, cumpr, /table) 
PM, r ; PV-WAVE prints the following:
; 14
; 9
; 12
; 10
; 12