RANDOM Function
Generates pseudorandom numbers. The default distribution is a uniform (0, 1) distribution, but many different distributions can be specified through the use of keywords.
Usage
result = RANDOM(n)
Input Parameters
n—Number of random numbers to generate.
Returned Value
result—A one-dimensional array of length n containing the random numbers. If the keywords Mvar_Normal and Covariances are used, then a two-dimensional array is returned.
Keywords
A—Shape parameter of the Gamma distribution. Keyword A must be positive. Keywords A and Gamma both must be specified to force RANDOM to return random numbers from a Gamma distribution.
Beta—If present and nonzero, the random numbers are generated from a beta distribution. Keywords Beta, Pin, and Qin all must be specified to force RANDOM to return numbers from a beta distribution.
Covariances—Two-dimensional, square matrix containing the variance-covariance matrix. The two-dimensional array returned by RANDOM is of the following size:
n-by-N_ELEMENTS(Covariances(*, 0))
Keywords Mvar_Normal and Covariances must be specified to return numbers from a multivariate normal distribution.
Double—If present and nonzero, double precision is used.
Exponential—If present and nonzero, the random numbers are generated from a standard exponential distribution.
Gamma—If present and nonzero, the random numbers are generated form a standard Gamma distribution. Keywords Gamma and A both must be specified to force RANDOM to return random numbers from a Gamma distribution.
Mvar_Normal—If present and nonzero, the random numbers are generated from a multivariate normal distribution. Keywords Mvar_Normal and Covariances must be specified to return numbers from a multivariate normal distribution.
Normal—If present and nonzero, the random numbers are generated from a standard normal distribution using an inverse CDF method.
Pin—First parameter of the beta distribution. Keyword Pin must be positive. Keywords Pin, Qin, and Beta all must be specified to force RANDOM to return numbers from a beta distribution.
Poisson—If present and nonzero, the random numbers are generated from a Poisson distribution. Keywords Poisson and Theta both must be specified to force RANDOM to return random numbers from a Poisson distribution.
Qin—Second parameter of the beta distribution. Keyword Qin must be positive. Keywords Qin, Pin, and Beta all must be specified to force RANDOM to return numbers from a beta distribution.
Theta—Mean of the Poisson distribution. Keyword Theta must be positive. Keywords Theta and Poisson both must be specified to force RANDOM to return random numbers from a Poisson distribution.
Uniform—If present and nonzero, the random numbers are generated from a uniform (0, 1) distribution. The default action of this returns random numbers from a uniform (0, 1) distribution.
Discussion
RANDOM returns random numbers from any of a number of different distributions. The determination of which distribution to generate the random numbers from is based on the presence of a keyword, or group of keywords. If RANDOM is called without any keywords, then random numbers from a uniform (0, 1) distribution are returned.
Uniform (0,1) Distribution
The default action of RANDOM generates pseudorandom numbers from a uniform (0, 1) distribution using a multiplicative, congruent method. The form of the generator follows:
xi   cxi–1 mod(231–1)
Each xi is then scaled into the unit interval (0, 1). The possible values for c in the generators are 16807, 397204094, and 950706376. The selection is made by using the RANDOMOPT procedure with the Gen_Option keyword. The choice of 16807 results in the fastest execution time. If no selection is made explicitly, the functions use the multiplier 16807.
The RANDOMOPT procedure (RANDOMOPT Procedure) called with the Set keyword is used to initialize the seed of the random-number generator.
You can select a shuffled version of these generators. In this scheme, a table is filled with the first 128 uniform (0,1) numbers resulting from the simple multiplicative congruent generator. Then, for each xi from the simple generator, the low-order bits of xi are used to select a random integer, j, from 1 to 128. The j-th entry in the table is then delivered as the random number, and xi, after being scaled into the unit interval, is inserted into the j-th position in the table.
The values returned are positive and less than 1.0. Some values returned may be smaller than the smallest relative spacing; however, it may be the case that some value, for example r(i), is such that 1.0 – r(i) = 1.0.
Deviates from the distribution with uniform density over the interval (a, b) can be obtained by scaling the output.
Normal Distribution
Calling RANDOM with keyword Normal generates pseudorandom numbers from a standard normal (Gaussian) distribution using an inverse CDF technique. In this method, a uniform (0, 1) random deviate is generated. Then, the inverse of the normal distribution function is evaluated at that point using the PV-WAVE function NORMALCDF with keyword Inverse.
Deviates from the normal distribution with mean specific mean and standard deviation can be obtained by scaling the output from RANDOM.
Exponential Distribution
Calling RANDOM with keyword Exponential generates pseudorandom numbers from a standard exponential distribution. The probability density function is f(x) = e–x, for x > 0. RANDOM uses an antithetic inverse CDF technique. In other words, a uniform random deviate U is generated, and the inverse of the exponential cumulative distribution function is evaluated at 1.0 – U to yield the exponential deviate.
Poisson Distribution
Calling RANDOM with keywords Poisson and Theta generates pseudorandom numbers from a Poisson distribution with positive mean Theta. The probability function (with θ = Theta) follows:
If Theta is less than 15, RANDOM uses an inverse CDF method; otherwise, the PTPE method of Schmeiser and Kachitvichyanukul (1981) is used. (See Schmeiser, 1983.) The PTPE method uses a composition of four regions, a triangle, a parallelogram, and two negative exponentials. In each region except the triangle, acceptance/rejection is used. The execution time of the method is essentially insensitive to the mean of the Poisson.
Gamma Distribution
Calling RANDOM with keywords Gamma and A generates pseudorandom numbers from a Gamma distribution with shape parameter a = A and unit scale parameter. The probability density function follows:
Various computational algorithms are used depending on the value of the shape parameter a. For the special case of a = 0.5, squared and halved normal deviates are used; for the special case of a = 1.0, exponential deviates are generated. Otherwise, if a is less than 1.0, an acceptance-rejection method due to Ahrens, described in Ahrens and Dieter (1974), is used. If a is greater than 1.0, a 10-region rejection procedure developed by Schmeiser and Lal (1980) is used.
The Erlang distribution is a standard Gamma distribution with the shape parameter having a value equal to a positive integer; hence, RANDOM generates pseudorandom deviates from an Erlang distribution with no modifications required.
Beta Distribution
Calling RANDOM with keywords Beta, Pin, and Qin generates pseudorandom numbers from a beta distribution with parameters Pin and Qin, both of which must be positive. With p = Pin  and q = Qin, the probability density function is:
where Γ() is the Gamma function.
The algorithm used depends on the values of p and q. Except for the trivial cases of p = 1 or  q = 1, in which the inverse CDF method is used, all the methods use acceptance/rejection. If p and q are both less than 1, the method of Jöhnk (1964) is used. If either p or q is less than 1 and the other is greater than 1, the method of Atkinson (1979) is used. If both p and q are greater than 1, algorithm BB of Cheng (1978), which requires very little setup time, is used if x is less than 4, and algorithm B4PE of Schmeiser and Babu (1980) is used if x is greater than or equal to 4.
 
note
Note that for p and q both greater than 1, calling RANDOM to generate random numbers from a beta distribution a loop getting less than four variates on each call yields the same set of deviates as executing one call and getting all the deviates at once.
The values returned are less than 1.0 and greater than ε, where ε is the smallest positive number such that  1.0 – ε < 1.0.
Multivariate Normal Distribution
Calling RANDOM with keywords Mvar_Normal and Covariances generates pseudorandom numbers from a multivariate normal distribution with mean array consisting of all zeros and variance-covariance matrix defined using keyword Covariances. First, the Cholesky factor of the variance-covariance matrix is computed. Then, independent random normal deviates with mean zero and variance 1 are generated, and the matrix containing these deviates is post-multiplied by the Cholesky factor. Because the Cholesky factorization is performed in each invocation, it is best to generate as many random arrays as needed at once.
Deviates from a multivariate normal distribution with means other than zero can be generated by using RANDOM with keywords Mvar_Normal and Covariances, then adding the arrays of means to each row of the result.
Example
In this example, RANDOM is used to generate five pseudorandom, uniform numbers. Since RANDOMOPT is not called, the generator used is a simple multiplicative congruential one with a multiplier of 16807.
; Set the random seed.
RANDOMOPT, Set = 123457
; Compute the random numbers.
r = RANDOM(5) 
PM, r
; PV-WAVE prints the following:
; 0.966220
; 0.260711
; 0.766262
; 0.569337
; 0.844829
See Also
In the PV‑WAVE Reference:
NORMALCDF
For Additional Information
Ahrens and Dieter, 1974.
Atkinson, 1979.
Cheng, 1978.
Schmeiser, 1983.
Schmeiser and Babu, 1980.
Schmeiser and Lal, 1980.
Schmeiser and Kachitvichyanukul, 1981.