|
JMSLTM Numerical Library 5.0.1 | |||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.Random com.imsl.stat.Random
public class Random
Generate uniform and non-uniform random number distributions.
The non-uniform distributions are generated from a uniform distribution.
By default, this class uses the uniform distribution generated by the
base class Random
. If the multiplier is set in this class
then a multiplicative congruential method is used. The form of the generator is
setMultiplier
.
Evidence suggests that the performance of 950706376 is best
among these three choices (Fishman and Moore 1982).
Alternatively, one can select a 32-bit or 64-bit Mersenne Twister
generator by first instantiating MersenneTwister
or MersenneTwister64
.
These generators have a period of and a 623-dimensional
equidistribution property. See Matsumoto et al. 1998 for details.
The generation of uniform (0,1) numbers is done by the method
nextDouble
.
Nonuniform random numbers are generated using a variety of transformation procedures. All of the transformations used are exact (mathematically). The most straightforward transformation is the inverse CDF technique, but it is often less efficient than others involving acceptance/rejection and mixtures. See Kennedy and Gentle(1980) for discussion of these and other techniques.
Many of the nonuniform generators use different algorithms depending on the values of the parameters of the distributions. This is particularly true of the generators for discrete distributions. Schmeiser (1983) gives an overview of techniques for generating deviates from discrete distributions.Extensive empirical tests of some of the uniform random number generators
available in the Random
class are reported by Fishman and Moore
(1982 and 1986). Results of tests on the generator using the multiplier 16807
are reported by Learmonth and Lewis (1973). If the user wishes to perform
additional tests, the routines in Chapter 17, Tests of Goodness of Fit,
may be of use. Often in Monte Carlo applications, it is appropriate to
construct an ad hoc test that is sensitive to departures that are important
in the given application. For example, in using Monte Carlo methods to evaluate
a one-dimensional integral, autocorrelations of order one may not be harmful,
but they may be disastrous in evaluating a two-dimensional integral. Although
generally the routines in this chapter for generating random deviates from
nonuniform distributions use exact methods, and, hence, their quality depends
almost solely on the quality of the underlying uniform generator, it is often
advisable to employ an ad hoc test of goodness of fit for the transformations
that are to be applied to the deviates from the nonuniform generator.
Nested Class Summary | |
---|---|
static interface |
Random.BaseGenerator
Base pseudorandom number. |
Constructor Summary | |
---|---|
Random()
Constructor for the Random number generator class. |
|
Random(long seed)
Constructor for the Random number generator class with supplied seed. |
|
Random(Random.BaseGenerator baseGenerator)
Constructor for the Random number generator class with an alternate basic number generator. |
Method Summary | |
---|---|
protected int |
next(int bits)
Generates the next pseudorandom number. |
double |
nextBeta(double p,
double q)
Generate a pseudorandom number from a beta distribution. |
int |
nextBinomial(int n,
double p)
Generate a pseudorandom number from a binomial distribution. |
double |
nextCauchy()
Generates a pseudorandom number from a Cauchy distribution. |
double |
nextChiSquared(double df)
Generates a pseudorandom number from a Chi-squared distribution. |
double |
nextExponential()
Generates a pseudorandom number from a standard exponential distribution. |
double |
nextExponentialMix(double theta1,
double theta2,
double p)
Generate a pseudorandom number from a mixture of two exponential distributions. |
double |
nextExtremeValue(double mu,
double beta)
Generate a pseudorandom number from an extreme value distribution. |
double |
nextF(double dfn,
double dfd)
Generate a pseudorandom number from the F distribution. |
double |
nextGamma(double a)
Generates a pseudorandom number from a standard gamma distribution. |
int |
nextGeometric(double p)
Generate a pseudorandom number from a geometric distribution. |
int |
nextHypergeometric(int n,
int m,
int l)
Generate a pseudorandom number from a hypergeometric distribution. |
int |
nextLogarithmic(double a)
Generate a pseudorandom number from a logarithmic distribution. |
double |
nextLogNormal(double mean,
double stdev)
Generate a pseudorandom number from a lognormal distribution. |
double[] |
nextMultivariateNormal(int k,
Cholesky matrix)
Generate pseudorandom numbers from a multivariate normal distribution. |
int |
nextNegativeBinomial(double rk,
double p)
Generate a pseudorandom number from a negative binomial distribution. |
double |
nextNormal()
Generate a pseudorandom number from a standard normal distribution using an inverse CDF method. |
double |
nextNormalAR()
Generate a pseudorandom number from a standard normal distribution using an acceptance/rejection method. |
int |
nextPoisson(double theta)
Generate a pseudorandom number from a Poisson distribution. |
double |
nextRayleigh(double alpha)
Generate a pseudorandom number from a Rayleigh distribution. |
double |
nextStudentsT(double df)
Generate a pseudorandom number from a Student's t distribution. |
double |
nextTriangular()
Generate a pseudorandom number from a triangular distribution on the interval (0,1). |
double |
nextVonMises(double c)
Generate a pseudorandom number from a von Mises distribution. |
double |
nextWeibull(double a)
Generate a pseudorandom number from a Weibull distribution. |
void |
setMultiplier(int multiplier)
Sets the multiplier for a linear congruential random number generator. |
void |
setSeed(long seed)
Sets the seed. |
void |
skip(int n)
Resets the seed to skip ahead in the base linear congruential generator. |
Methods inherited from class java.util.Random |
---|
nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Random()
public Random(long seed)
seed
- a long
which represents the random number
generator seedpublic Random(Random.BaseGenerator baseGenerator)
baseGenerator
- is used to override the method next
.Method Detail |
---|
protected int next(int bits)
next
method is used.
If the multiplier
is set then the multiplicative congruential
method is used. Otherwise, super.next(bits)
is used.
next
in class Random
bits
- is the number of random bits required.
public double nextBeta(double p, double q)
Method nextBeta
generates pseudorandom numbers from a
beta distribution with parameters p and
q, both of which must be positive. 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 of the methods use acceptance/rejection. If p and q are both less than 1, the method of Johnk (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.
The value returned is less than 1.0 and greater than , where is the smallest positive number such that is less than 1.0.
p
- a double
, the first beta distribution parameter, p > 0q
- a double
, the second beta distribution parameter, q > 0
double
, a pseudorandom number from
a beta distributionpublic int nextBinomial(int n, double p)
nextBinomial
generates pseudorandom numbers from a
binomial distribution with parameters n and
p. n and p
must be positive, and p must be less than
1. The probability function (with
n = n and p = p) is
for .
The algorithm used depends on the values of n and p. If or if p is less than a machine epsilon, the inverse CDF technique is used; otherwise, the BTPE algorithm of Kachitvichyanukul and Schmeiser (see Kachitvichyanukul 1982) is used. This is an acceptance/rejection method using a composition of four regions. (TPE equals Triangle, Parallelogram, Exponential, left and right.)
n
- an int
, the number of Bernoulli trials.p
- a double
, the probability of success on
each trial, .
int
, the pseudorandom number from a
binomial distribution.public double nextCauchy()
Use of the inverse CDF technique would yield a Cauchy deviate from a
uniform (0, 1) deviate, u, as
.
Rather than evaluating a tangent directly, however, nextCauchy
generates two uniform (-1, 1) deviates,
and . These values can
be thought of as sine and cosine values. If
is less than or equal to 1, then is delivered as the Cauchy deviate; otherwise, and are rejected and two new uniform (-1, 1) deviates are generated. This method is also equivalent to taking the ratio of two independent normal deviates.
Deviates from the Cauchy distribution with median t and first quartile t - s, that is, with density
can be obtained by scaling the output from nextCauchy
.
To do this, first scale the output from nextCauchy
by
S and then add T to the
result.
double
, a pseudorandom number from a Cauchy distributionpublic double nextChiSquared(double df)
nextChiSquared
generates pseudorandom numbers from a
chi-squared distribution with df
degrees of freedom. If
df
is an even integer less than 17, the chi-squared deviate
r is generated as
where and the
are independent random deviates from a uniform
(0, 1) distribution. If df
is an odd
integer less than 17, the chi-squared deviate is
generated in the same way, except the square of a normal deviate is
added to the expression above. If df
is greater than
16 or is not an integer, and if it is not too large
to cause overflow in the gamma random number generator, the chi-squared
deviate is generated as a special case of a gamma deviate, using
nextGamma
. If overflow would occur in nextGamma
,
the chi-squared deviate is generated in the manner described above,
using the logarithm of the product of uniforms, but scaling the
quantities to prevent underflow and overflow.
df
- a double
which specifies the number of degrees
of freedom. It must be positive.
double
, a pseudorandom number from a Chi-squared distribution.public double nextExponential()
nextExponential
uses an antithetic inverse CDF
technique; that is, 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.
Deviates from the exponential distribution with mean
can be generated by using nextExponential
and then multiplying the result by .
double
which specifies a pseudorandom number from a
standard exponential distributionpublic double nextExponentialMix(double theta1, double theta2, double p)
where , , and .
In the case of a convex mixture, that is, the case , the mixing parameter p is
interpretable as a probability; and nextExponentialMix
with
probability p generates an exponential deviate
with mean , and with probability
1 - p generates an exponential with mean
. When p is greater
than 1, but less than ,
then either an exponential deviate with mean
or the sum of two exponentials with means
and is generated. The probabilities are
and
1 - q, respectively, for the single exponential
and the sum of the two exponentials.
theta1
- a double
which specifies the mean of the
exponential distribution that has the larger mean.theta2
- a double
which specifies the mean of the
exponential distribution that has the smaller mean.
theta2
must be positive and less than or equal to theta1
.p
- a double
which specifies the mixing parameter. It
must satisfy .
double
, a pseudorandom number from a mixture
of the two exponential distributions.public double nextExtremeValue(double mu, double beta)
mu
- a double
scalar value representing the location parameter.beta
- a double
scalar value representing the scale parameter.
double
pseudorandom number from
an extreme value distributionpublic double nextF(double dfn, double dfd)
dfn
- a double
, the numerator degrees
of freedom. It must be positive.dfd
- a double
, the denominator degrees
of freedom. It must be positive.
double
, a pseudorandom number from
an F distributionpublic double nextGamma(double a)
Method nextGamma
generates pseudorandom numbers from a
gamma distribution with shape parameter a. The
probability density function is
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; and for the special case of a = 1.0,
exponential deviates (from method nextExponential
) are
used. 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 ten-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,
nextGamma
generates pseudorandom deviates from an Erlang
distribution with no modifications required.
a
- a double
, the shape parameter of the gamma distribution.
It must be positive.
double
, a pseudorandom number from a standard gamma
distributionpublic int nextGeometric(double p)
nextGeometric
generates pseudorandom numbers from a
geometric distribution with parameter p, where
P =p is the probability of getting a success on any
trial. A geometric deviate can be interpreted as the number of trials
until the first success (including the trial in which the first success
is obtained). The probability function is
for and
.The geometric distribution as defined above has mean 1/P.
The i-th geometric deviate is generated as the smallest integer not less than , where the are independent uniform (0, 1) random numbers (see Knuth, 1981).
The geometric distribution is often defined on 0, 1, 2, ..., with mean (1 - P)/P. Such deviates can be obtained by subtracting 1 from each element returned value.
p
- a double
, the probability of success on each
trial, .
int
, a pseudorandom number from a geometric
distribution.public int nextHypergeometric(int n, int m, int l)
Method nextHypergeometric
generates pseudorandom numbers
from a hypergeometric distribution with parameters n,
m, and l. The hypergeometric
random variable x can be thought of as the number
of items of a given type in a random sample of size n
that is drawn without replacement from a population of size
l containing m items of this
type. The probability function is
for .
If the hypergeometric
probability function with
parameters n, m, and
l evaluated at n - l + m
(or at 0 if this is negative) is greater than the machine epsilon, and
less than 1.0 minus the machine epsilon, then
nextHypergeometric
uses the inverse CDF technique. The
method recursively computes the hypergeometric
probabilities, starting at
and using the ratio f (x = x + 1)/f(x = x) (see
Fishman 1978, page 457).
If the hypergeometric
probability function is too small
or too close to 1.0, then
nextHypergeometric
generates integer deviates uniformly in
the interval , for ; and at the I-th step, if the generated deviate
is less than or equal to the number of special items remaining in the
lot, the occurrence of one special item is tallied and the number of
remaining special items is decreased by one. This process continues
until the sample size or the number of special items in the lot is
reached, whichever comes first. This method can be much slower than the
inverse CDF technique. The timing depends on n.
If n is more than half of l
(which in practical examples is rarely the case), the user may wish to
modify the problem, replacing n by
l - n, and to consider the deviates to be the
number of special items not included in the sample.
n
- an int
which specifies the number of items in the sample, n > 0m
- an int
which specifies the number of special items in the
population, or lot, m > 0l
- an int
which specifies the number of items in the lot, l > max(n,m)
int
which specifies the number of special items in a sample of
size n drawn without replacement from a population of size l that contains
m such special items.public int nextLogarithmic(double a)
Method nextLogarithmic
generates pseudorandom numbers
from a logarithmic distribution with parameter a.
The probability function is
for , and .
The methods used are described by Kemp (1981) and depend on the value of a. If a is less than 0.95, Kemp's algorithm LS, which is a "chop-down" variant of an inverse CDF technique, is used. Otherwise, Kemp's algorithm LK, which gives special treatment to the highly probable values of 1 and 2, is used.
a
- a double
which specifies the parameter of the
logarithmic distribution, .
int
, a pseudorandom number from a logarithmic distribution.public double nextLogNormal(double mean, double stdev)
Method nextLogNormal
generates pseudorandom numbers from
a lognormal distribution with parameters mean
and
stdev
. The scale parameter in the underlying normal
distribution, stdev
, must be positive. The method is to
generate normal deviates with mean mean
and standard
deviation stdev
and then to exponentiate the normal deviates.
With and , the probability density function for the lognormal distribution is
The mean and variance of the lognormal distribution are and , respectively.
mean
- a double
which specifies the mean of the underlying
normal distributionstdev
- a double
which specifies the standard deviation of
the underlying normal distribution. It must be positive.
double
, a pseudorandom number from a lognormal distributionpublic double[] nextMultivariateNormal(int k, Cholesky matrix)
nextMultivariateNormal
generates pseudorandom numbers
from a multivariate normal distribution with mean vector consisting of
all zeroes and variance-covariance matrix whose Cholesky factor (or
"square root") is matrix
; that is, matrix
is
an upper triangular matrix such that the transpose of matrix
times matrix
is the variance-covariance matrix. First,
independent random normal deviates with mean 0 and
variance 1 are generated, and then the matrix
containing these deviates is post-multiplied by matrix
.
Deviates from a multivariate normal distribution with means other
than zero can be generated by using nextMultivariateNormal
and then by adding the means to the deviates.
k
- an int
which specifies the length of the multivariate
normal vectorsmatrix
- is the Cholesky
factorization of the variance-covariance
matrix of order k
double
array which contains the pseudorandom numbers from a
multivariate normal distributionpublic int nextNegativeBinomial(double rk, double p)
Method nextNegativeBinomial
generates pseudorandom
numbers from a negative binomial distribution with parameters
and .
and must be
positive and p must be less than 1. The probability
function with ( and
) is
for .
If r is an integer, the distribution is often called the Pascal distribution and can be thought of as modeling the length of a sequence of Bernoulli trials until r successes are obtained, where p is the probability of getting a success on any trial. In this form, the random variable takes values r, r + 1, and can be obtained from the negative binomial random variable defined above by adding r to the negative binomial variable. This latter form is also equivalent to the sum of r geometric random variables defined as taking values .
If rp/(1 - p) is less than 100
and is greater than the machine epsilon,
nextNegativeBinomial
uses the inverse CDF technique;
otherwise, for each negative binomial deviate, nextNegativeBinomial
generates a gamma (r, p/(1 - p)) deviate
y and then generates a Poisson deviate with
parameter y.
rk
- a double
which specifies the negative binomial
parameter, rk > 0p
- a double
which specifies the probability of success
on each trial. It must be greater than machine precision
and less than one.
int
which specifies the pseudorandom number
from a negative binomial distribution. If rk is an integer,
the deviate can be thought of as the number of failures in a
sequence of Bernoulli trials before rk successes occur.public double nextNormal()
inverseNormal
. This method is slower than the
acceptance/rejection technique used in the nextNormalAR
to
generate standard normal deviates. Deviates from the normal distribution
with mean and standard deviation
can be obtained by scaling the output from
nextNormal
. To do this first scale the output of
nextNormal
by and then add
to the result.
double
which represents a pseudorandom number from
a standard normal distributionpublic double nextNormalAR()
nextNormalAR
generates pseudorandom numbers from a
standard normal (Gaussian) distribution using an acceptance/rejection
technique due to Kinderman and Ramage (1976). In this method, the normal
density is represented as a mixture of densities over which a variety of
acceptance/rejection methods due to Marsaglia (1964), Marsaglia and Bray
(1964), and Marsaglia, MacLaren, and Bray (1964) are applied. This method
is faster than the inverse CDF technique used in nextNormal
to generate standard normal deviates.
Deviates from the normal distribution with mean
and standard deviation can be obtained by
scaling the output from nextNormalAR
. To do this first
scale the output of nextNormalAR
by
and then add to the result.
double
which represents a pseudorandom number from
a standard normal distributionpublic int nextPoisson(double theta)
Method nextPoisson
generates pseudorandom numbers from
a Poisson distribution with parameter theta
. theta
,
which is the mean of the Poisson random variable, must be positive. The
probability function (with ) is
for
If theta
is less than 15,
nextPoisson
uses an inverse CDF method; otherwise the
PTPE
method of Schmeiser and Kachitvichyanukul (1981) (see
also Schmeiser 1983) is used.
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.
theta
- a double
which specifies the mean of the Poisson
distribution, theta > 0
int
, a pseudorandom number from a Poisson distributionpublic double nextRayleigh(double alpha)
Method nextRayleigh
generates pseudorandom numbers from a
Rayleigh distribution with scale parameter alpha.
alpha
- a double
which specifies the scale parameter of
the Rayleigh distribution
double
, a pseudorandom number from a
Rayleigh distributionpublic double nextStudentsT(double df)
nextStudentsT
generates pseudo-random numbers from a
Student's t distribution with df
degrees of freedom, using a method suggested by Kinderman, Monahan, and
Ramage (1977). The method ("TMX" in the reference) involves a
representation of the t density as the sum of a
triangular density over (-2, 2) and the difference
of this and the t density. The mixing probabilities
depend on the degrees of freedom of the t
distribution. If the triangular density is chosen, the variate is
generated as the sum of two uniforms; otherwise, an acceptance/rejection
method is used to generate a variate from the difference density.
For degrees of freedom less than 100, nextStudentsT
requires approximately twice the execution time as
nextNormalAR
, which generates pseudorandom normal deviates.
The execution time of nextStudentsT
increases very slowly
as the degrees of freedom increase. Since for very large degrees of
freedom the normal distribution and the t
distribution are very similar, the user may find that the difference in
the normal and the t does not warrant the
additional generation time required to use nextStudentsT
instead of nextNormalAR
.
df
- a double
which specifies the number of degrees
of freedom. It must be positive.
double
, a pseudorandom number from a
Student's t distributionpublic double nextTriangular()
nextTriangular
uses an
inverse CDF technique.
double
, a pseudorandom number from a
triangular distribution on the interval (0,1)public double nextVonMises(double c)
Method nextVonMises
generates pseudorandom numbers from
a von Mises distribution with parameter c, which
must be positive. With c = C, the probability
density function is
where is the modified Bessel function of the first kind of order 0. The probability density equals 0 outside the interval .
The algorithm is an acceptance/rejection method using a wrapped Cauchy distribution as the majorizing distribution. It is due to Best and Fisher (1979).
c
- a double
which specifies the parameter of the
von Mises distribution, .
double
, a pseudorandom number from a
von Mises distributionpublic double nextWeibull(double a)
Method nextWeibull
generates pseudorandom numbers from a
Weibull distribution with shape parameter a.
The probability density function is
nextWeibull
uses an antithetic inverse CDF technique to
generate a Weibull variate; that is, a uniform random deviate
U is generated and the inverse of the Weibull
cumulative distribution function is evaluated at 1.0 - u
to yield the Weibull deviate.
Deviates from the two-parameter Weibull distribution, with shape
parameter a and scale parameter b, can be generated by using
nextWeibull
and then multiplying the result by
b.
The Rayleigh distribution with probability density function,
is the same as a Weibull distribution with shape parameter a equal to 2 and scale parameter b equal to
.hence, nextWeibull
and simple multiplication can be
used to generate Rayleigh deviates.
a
- a double
which specifies the shape parameter of
the Weibull distribution, a > 0
double
, a pseudorandom number from a
Weibull distributionpublic void setMultiplier(int multiplier)
java.util.Random
,
is replaced by the generator
multiplier
- an int
which represents the random number
generator multiplierpublic void setSeed(long seed)
setSeed
in class Random
seed
- a long
which represents the random number
generator seedpublic void skip(int n)
setMultiplier
.
The method skips ahead in the deviates returned by the protected method
next
. The public methods use next(int)
as their source of uniform random deviates. Some methods call it more than
once. For instance, each call to nextDouble
calls it
twice.
n
- is the number of random deviates to skip.
|
JMSLTM Numerical Library 5.0.1 | |||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |