| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes functions for generating random variates and computing their probability distributions. Samples from the distributions described in this chapter can be obtained using any of the random number generators in the library as an underlying source of randomness.
In the simplest cases a non-uniform distribution can be obtained analytically from the uniform distribution of a random number generator by applying an appropriate transformation. This method uses one call to the random number generator. More complicated distributions are created by the acceptance-rejection method, which compares the desired distribution against a distribution which is similar and known analytically. This usually requires several samples from the generator.
The library also provides cumulative distribution functions and inverse cumulative distribution functions, sometimes referred to as quantile functions. The cumulative distribution functions and their inverses are computed separately for the upper and lower tails of the distribution, allowing full accuracy to be retained for small results.
The functions for random variates and probability density functions described in this section are declared in ‘gsl_randist.h’. The corresponding cumulative distribution functions are declared in ‘gsl_cdf.h’.
Note that the discrete random variate functions always
return a value of type unsigned int, and on most platforms this
has a maximum value of
. They should only be called with
a safe range of parameters (where there is a negligible probability of
a variate exceeding this limit) to prevent incorrect results due to
overflow.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Continuous random number distributions are defined by a probability
density function,
, such that the probability of
occurring in the infinitesimal range
to
is
.
The cumulative distribution function for the lower tail
is
defined by the integral,
and gives the probability of a variate taking a value less than
.
The cumulative distribution function for the upper tail
is
defined by the integral,
and gives the probability of a variate taking a value greater than
.
The upper and lower cumulative distribution functions are related by
and satisfy
,
.
The inverse cumulative distributions,
and
give the values of
which correspond to a specific value of
or
.
They can be used to find confidence limits from probability values.
For discrete distributions the probability of sampling the integer
value
is given by
, where
.
The cumulative distribution for the lower tail
of a
discrete distribution is defined as,
where the sum is over the allowed range of the distribution less than
or equal to
.
The cumulative distribution for the upper tail of a discrete
distribution
is defined as
giving the sum of probabilities for all values greater than
.
These two definitions satisfy the identity
.
If the range of the distribution is 1 to
inclusive then
,
while
,
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a Gaussian random variate, with mean zero and
standard deviation sigma. The probability distribution for
Gaussian random variates is,
for
in the range
to
. Use the
transformation
on the numbers returned by
gsl_ran_gaussian to obtain a Gaussian distribution with mean
. This function uses the Box-Mueller algorithm which requires two
calls to the random number generator r.
This function computes the probability density
at x
for a Gaussian distribution with standard deviation sigma, using
the formula given above.
This function computes a Gaussian random variate using the alternative Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods. The Ziggurat algorithm is the fastest available algorithm in most cases.
These functions compute results for the unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, sigma = 1.
These functions compute the cumulative distribution functions
,
and their inverses for the Gaussian
distribution with standard deviation sigma.
These functions compute the cumulative distribution functions
,
and their inverses for the unit Gaussian
distribution.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function provides random variates from the upper tail of a Gaussian distribution with standard deviation sigma. The values returned are larger than the lower limit a, which must be positive. The method is based on Marsaglia's famous rectangle-wedge-tail algorithm (Ann. Math. Stat. 32, 894–899 (1961)), with this aspect explained in Knuth, v2, 3rd ed, p139,586 (exercise 11).
The probability distribution for Gaussian tail random variates is,
for
where
is the normalization constant,
This function computes the probability density
at x
for a Gaussian tail distribution with standard deviation sigma and
lower limit a, using the formula given above.
These functions compute results for the tail of a unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, sigma = 1.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function generates a pair of correlated Gaussian variates, with
mean zero, correlation coefficient rho and standard deviations
sigma_x and sigma_y in the
and
directions.
The probability distribution for bivariate Gaussian random variates is,
for
in the range
to
. The
correlation coefficient rho should lie between
and
.
This function computes the probability density
at
(x,y) for a bivariate Gaussian distribution with standard
deviations sigma_x, sigma_y and correlation coefficient
rho, using the formula given above.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the exponential distribution
with mean mu. The distribution is,
for
.
This function computes the probability density
at x
for an exponential distribution with mean mu, using the formula
given above.
These functions compute the cumulative distribution functions
,
and their inverses for the exponential
distribution with mean mu.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Laplace distribution
with width a. The distribution is,
for
.
This function computes the probability density
at x
for a Laplace distribution with width a, using the formula
given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Laplace
distribution with width a.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the exponential power distribution
with scale parameter a and exponent b. The distribution is,
for
. For
this reduces to the Laplace
distribution. For
it has the same form as a gaussian
distribution, but with
.
This function computes the probability density
at x
for an exponential power distribution with scale parameter a
and exponent b, using the formula given above.
These functions compute the cumulative distribution functions
,
for the exponential power distribution with
parameters a and b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Cauchy distribution with
scale parameter a. The probability distribution for Cauchy
random variates is,
for
in the range
to
. The Cauchy
distribution is also known as the Lorentz distribution.
This function computes the probability density
at x
for a Cauchy distribution with scale parameter a, using the formula
given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Cauchy
distribution with scale parameter a.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Rayleigh distribution with
scale parameter sigma. The distribution is,
for
.
This function computes the probability density
at x
for a Rayleigh distribution with scale parameter sigma, using the
formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Rayleigh
distribution with scale parameter sigma.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the tail of the Rayleigh
distribution with scale parameter sigma and a lower limit of
a. The distribution is,
for
.
This function computes the probability density
at x
for a Rayleigh tail distribution with scale parameter sigma and
lower limit a, using the formula given above.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Landau distribution. The probability distribution for Landau random variates is defined analytically by the complex integral, For numerical purposes it is more convenient to use the following equivalent form of the integral,
This function computes the probability density
at x
for the Landau distribution using an approximation to the formula given
above.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Levy symmetric stable
distribution with scale c and exponent alpha. The symmetric
stable probability distribution is defined by a fourier transform,
There is no explicit solution for the form of
and the
library does not define a corresponding pdf function. For
the distribution reduces to the Cauchy distribution. For
it is a Gaussian distribution with
. For
the tails of the
distribution become extremely wide.
The algorithm only works for
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Levy skew stable
distribution with scale c, exponent alpha and skewness
parameter beta. The skewness parameter must lie in the range
. The Levy skew stable probability distribution is defined
by a fourier transform,
When
the term
is replaced by
. There is no explicit solution for the form of
and the library does not define a corresponding pdf
function. For
the distribution reduces to a Gaussian
distribution with
and the skewness parameter has no effect.
For
the tails of the distribution become extremely
wide. The symmetric distribution corresponds to
.
The algorithm only works for
.
The Levy alpha-stable distributions have the property that if
alpha-stable variates are drawn from the distribution
then the sum
will also be
distributed as an alpha-stable variate,
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the gamma
distribution. The distribution function is,
for
.
The gamma distribution with an integer parameter a is known as the Erlang distribution.
The variates are computed using the Marsaglia-Tsang fast gamma method.
This function for this method was previously called
gsl_ran_gamma_mt and can still be accessed using this name.
This function returns a gamma variate using the algorithms from Knuth (vol 2).
This function computes the probability density
at x
for a gamma distribution with parameters a and b, using the
formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the gamma
distribution with parameters a and b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the flat (uniform)
distribution from a to b. The distribution is,
if
and 0 otherwise.
This function computes the probability density
at x
for a uniform distribution from a to b, using the formula
given above.
These functions compute the cumulative distribution functions
,
and their inverses for a uniform distribution
from a to b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the lognormal
distribution. The distribution function is,
for
.
This function computes the probability density
at x
for a lognormal distribution with parameters zeta and sigma,
using the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the lognormal
distribution with parameters zeta and sigma.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The chi-squared distribution arises in statistics. If
are
independent gaussian random variates with unit variance then the
sum-of-squares,
has a chi-squared distribution with
degrees of freedom.
This function returns a random variate from the chi-squared distribution
with nu degrees of freedom. The distribution function is,
for
.
This function computes the probability density
at x
for a chi-squared distribution with nu degrees of freedom, using
the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the chi-squared
distribution with nu degrees of freedom.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The F-distribution arises in statistics. If
and
are chi-squared deviates with
and
degrees of
freedom then the ratio,
has an F-distribution
.
This function returns a random variate from the F-distribution with degrees of freedom nu1 and nu2. The distribution function is,
for
.
This function computes the probability density
at x
for an F-distribution with nu1 and nu2 degrees of freedom,
using the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the F-distribution
with nu1 and nu2 degrees of freedom.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The t-distribution arises in statistics. If
has a normal
distribution and
has a chi-squared distribution with
degrees of freedom then the ratio,
has a t-distribution
with
degrees of freedom.
This function returns a random variate from the t-distribution. The
distribution function is,
for
.
This function computes the probability density
at x
for a t-distribution with nu degrees of freedom, using the formula
given above.
These functions compute the cumulative distribution functions
,
and their inverses for the t-distribution
with nu degrees of freedom.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the beta
distribution. The distribution function is,
for
.
This function computes the probability density
at x
for a beta distribution with parameters a and b, using the
formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the beta
distribution with parameters a and b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the logistic
distribution. The distribution function is,
for
.
This function computes the probability density
at x
for a logistic distribution with scale parameter a, using the
formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the logistic
distribution with scale parameter a.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Pareto distribution of
order a. The distribution function is,
for
.
This function computes the probability density
at x
for a Pareto distribution with exponent a and scale b, using
the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Pareto
distribution with exponent a and scale b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The spherical distributions generate random vectors, located on a spherical surface. They can be used as random directions, for example in the steps of a random walk.
This function returns a random direction vector
=
(x,y) in two dimensions. The vector is normalized such that
. The obvious way to do this is to take a
uniform random number between 0 and
and let x and
y be the sine and cosine respectively. Two trig functions would
have been expensive in the old days, but with modern hardware
implementations, this is sometimes the fastest way to go. This is the
case for the Pentium (but not the case for the Sun Sparcstation).
One can avoid the trig evaluations by choosing x and
y in the interior of a unit circle (choose them at random from the
interior of the enclosing square, and then reject those that are outside
the unit circle), and then dividing by
.
A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd
ed, p140, exercise 23), requires neither trig nor a square root. In
this approach, u and v are chosen at random from the
interior of a unit circle, and then
and
.
This function returns a random direction vector
=
(x,y,z) in three dimensions. The vector is normalized
such that
. The method employed is
due to Robert E. Knop (CACM 13, 326 (1970)), and explained in Knuth, v2,
3rd ed, p136. It uses the surprising fact that the distribution
projected along any axis is actually uniform (this is only true for 3
dimensions).
This function returns a random direction vector
in n dimensions. The vector is normalized
such that
. The method
uses the fact that a multivariate gaussian distribution is spherically
symmetric. Each component is generated to have a gaussian distribution,
and then the components are normalized. The method is described by
Knuth, v2, 3rd ed, p135–136, and attributed to G. W. Brown, Modern
Mathematics for the Engineer (1956).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Weibull distribution. The
distribution function is,
for
.
This function computes the probability density
at x
for a Weibull distribution with scale a and exponent b,
using the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Weibull
distribution with scale a and exponent b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Type-1 Gumbel
distribution. The Type-1 Gumbel distribution function is,
for
.
This function computes the probability density
at x
for a Type-1 Gumbel distribution with parameters a and b,
using the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Type-1 Gumbel
distribution with parameters a and b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random variate from the Type-2 Gumbel
distribution. The Type-2 Gumbel distribution function is,
for
.
This function computes the probability density
at x
for a Type-2 Gumbel distribution with parameters a and b,
using the formula given above.
These functions compute the cumulative distribution functions
,
and their inverses for the Type-2 Gumbel
distribution with parameters a and b.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns an array of K random variates from a Dirichlet
distribution of order K-1. The distribution function is
for
and
. The delta function ensures that
.
The normalization factor
is
The random variates are generated by sampling K values
from gamma distributions with parameters
,
and renormalizing.
See A.M. Law, W.D. Kelton, Simulation Modeling and Analysis (1991).
This function computes the probability density
at theta[K] for a Dirichlet distribution with parameters
alpha[K], using the formula given above.
This function computes the logarithm of the probability density
for a Dirichlet distribution with parameters
alpha[K].
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Given
discrete events with different probabilities
,
produce a random value
consistent with its probability.
The obvious way to do this is to preprocess the probability list by
generating a cumulative probability array with
elements:
Note that this construction produces
. Now choose a
uniform deviate
between 0 and 1, and find the value of
such that
.
Although this in principle requires of order
steps per
random number generation, they are fast steps, and if you use something
like
as a starting point, you can often do
pretty well.
But faster methods have been devised. Again, the idea is to preprocess
the probability list, and save the result in some form of lookup table;
then the individual calls for a random discrete event can go rapidly.
An approach invented by G. Marsaglia (Generating discrete random numbers
in a computer, Comm ACM 6, 37–38 (1963)) is very clever, and readers
interested in examples of good algorithm design are directed to this
short and well-written paper. Unfortunately, for large
,
Marsaglia's lookup table can be quite large.
A much better approach is due to Alastair J. Walker (An efficient method
for generating discrete random variables with general distributions, ACM
Trans on Mathematical Software 3, 253–256 (1977); see also Knuth, v2,
3rd ed, p120–121,139). This requires two lookup tables, one floating
point and one integer, but both only of size
. After
preprocessing, the random numbers are generated in O(1) time, even for
large
. The preprocessing suggested by Walker requires
effort, but that is not actually necessary, and the
implementation provided here only takes
effort. In general,
more preprocessing leads to faster generation of the individual random
numbers, but a diminishing return is reached pretty early. Knuth points
out that the optimal preprocessing is combinatorially difficult for
large
.
This method can be used to speed up some of the discrete random number
generators below, such as the binomial distribution. To use it for
something like the Poisson Distribution, a modification would have to
be made, since it only takes a finite set of
outcomes.
This function returns a pointer to a structure that contains the lookup
table for the discrete random number generator. The array P[] contains
the probabilities of the discrete events; these array elements must all be
positive, but they needn't add up to one (so you can think of them more
generally as “weights”)—the preprocessor will normalize appropriately.
This return value is used
as an argument for the gsl_ran_discrete function below.
After the preprocessor, above, has been called, you use this function to get the discrete random numbers.
Returns the probability
of observing the variable k.
Since
is not stored as part of the lookup table, it must be
recomputed; this computation takes
, so if K is large
and you care about the original array
used to create the
lookup table, then you should just keep this original array
around.
De-allocates the lookup table pointed to by g.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the Poisson distribution
with mean mu. The probability distribution for Poisson variates is,
for
.
This function computes the probability
of obtaining k
from a Poisson distribution with mean mu, using the formula
given above.
These functions compute the cumulative distribution functions
,
for the Poisson distribution with parameter
mu.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns either 0 or 1, the result of a Bernoulli trial with probability p. The probability distribution for a Bernoulli trial is,
This function computes the probability
of obtaining
k from a Bernoulli distribution with probability parameter
p, using the formula given above.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the binomial distribution,
the number of successes in n independent trials with probability
p. The probability distribution for binomial variates is,
for
.
This function computes the probability
of obtaining k
from a binomial distribution with parameters p and n, using
the formula given above.
These functions compute the cumulative distribution functions
,
for the binomial
distribution with parameters p and n.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function computes a random sample n[] from the multinomial
distribution formed by N trials from an underlying distribution
p[K]. The distribution function for n[] is,
where
are nonnegative integers with
,
and
is a probability distribution with
.
If the array p[K] is not normalized then its entries will be
treated as weights and normalized appropriately. The arrays n[]
and p[] must both be of length K.
Random variates are generated using the conditional binomial method (see C.S. David, The computer generation of multinomial random variates, Comp. Stat. Data Anal. 16 (1993) 205–217 for details).
This function computes the probability
of sampling n[K] from a multinomial distribution
with parameters p[K], using the formula given above.
This function returns the logarithm of the probability for the
multinomial distribution
with parameters p[K].
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the negative binomial
distribution, the number of failures occurring before n successes
in independent trials with probability p of success. The
probability distribution for negative binomial variates is,
Note that
is not required to be an integer.
This function computes the probability
of obtaining k
from a negative binomial distribution with parameters p and
n, using the formula given above.
These functions compute the cumulative distribution functions
,
for the negative binomial distribution with
parameters p and n.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the Pascal distribution. The
Pascal distribution is simply a negative binomial distribution with an
integer value of
.
for
This function computes the probability
of obtaining k
from a Pascal distribution with parameters p and
n, using the formula given above.
These functions compute the cumulative distribution functions
,
for the Pascal distribution with
parameters p and n.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the geometric distribution,
the number of independent trials with probability p until the
first success. The probability distribution for geometric variates
is,
for
. Note that the distribution begins with
with this
definition. There is another convention in which the exponent
is replaced by
.
This function computes the probability
of obtaining k
from a geometric distribution with probability parameter p, using
the formula given above.
These functions compute the cumulative distribution functions
,
for the geometric distribution with parameter
p.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the hypergeometric
distribution. The probability distribution for hypergeometric
random variates is,
where
and
. The domain of
is
.
If a population contains
elements of “type 1” and
elements of “type 2” then the hypergeometric
distribution gives the probability of obtaining
elements of
“type 1” in
samples from the population without
replacement.
This function computes the probability
of obtaining k
from a hypergeometric distribution with parameters n1, n2,
t, using the formula given above.
These functions compute the cumulative distribution functions
,
for the hypergeometric distribution with
parameters n1, n2 and t.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a random integer from the logarithmic
distribution. The probability distribution for logarithmic random variates
is,
for
.
This function computes the probability
of obtaining k
from a logarithmic distribution with probability parameter p,
using the formula given above.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions allow the shuffling and sampling of a set of objects. The algorithms rely on a random number generator as a source of randomness and a poor quality generator can lead to correlations in the output. In particular it is important to avoid generators with a short period. For more information see Knuth, v2, 3rd ed, Section 3.4.2, “Random Sampling and Shuffling”.
This function randomly shuffles the order of n objects, each of
size size, stored in the array base[0..n-1]. The
output of the random number generator r is used to produce the
permutation. The algorithm generates all possible
permutations with equal probability, assuming a perfect source of random
numbers.
The following code shows how to shuffle the numbers from 0 to 51,
int a[52];
for (i = 0; i < 52; i++)
{
a[i] = i;
}
gsl_ran_shuffle (r, a, 52, sizeof (int));
|
This function fills the array dest[k] with k objects taken randomly from the n elements of the array src[0..n-1]. The objects are each of size size. The output of the random number generator r is used to make the selection. The algorithm ensures all possible samples are equally likely, assuming a perfect source of randomness.
The objects are sampled without replacement, thus each object can
only appear once in dest[k]. It is required that k be less
than or equal to n. The objects in dest will be in the
same relative order as those in src. You will need to call
gsl_ran_shuffle(r, dest, n, size) if you want to randomize the
order.
The following code shows how to select a random sample of three unique numbers from the set 0 to 99,
double a[3], b[100];
for (i = 0; i < 100; i++)
{
b[i] = (double) i;
}
gsl_ran_choose (r, a, 3, b, 100, sizeof (double));
|
This function is like gsl_ran_choose but samples k items
from the original array of n items src with replacement, so
the same object can appear more than once in the output sequence
dest. There is no requirement that k be less than n
in this case.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following program demonstrates the use of a random number generator to produce variates from a distribution. It prints 10 samples from the Poisson distribution with a mean of 3.
|
If the library and header files are installed under ‘/usr/local’ (the default location) then the program can be compiled with these options,
$ gcc -Wall demo.c -lgsl -lgslcblas -lm |
Here is the output of the program,
$ ./a.out2 5 5 2 1 0 3 4 1 1 |
The variates depend on the seed used by the generator. The seed for the
default generator type gsl_rng_default can be changed with the
GSL_RNG_SEED environment variable to produce a different stream
of variates,
$ GSL_RNG_SEED=123 ./a.outGSL_RNG_SEED=123 4 5 6 3 3 1 4 2 5 5 |
The following program generates a random walk in two dimensions.
|
Here is the output from the program, three 10-step random walks from the origin,
The following program computes the upper and lower cumulative
distribution functions for the standard normal distribution at
.
|
Here is the output of the program,
prob(x < 2.000000) = 0.977250 prob(x > 2.000000) = 0.022750 Pinv(0.977250) = 2.000000 Qinv(0.022750) = 2.000000 |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For an encyclopaedic coverage of the subject readers are advised to consult the book Non-Uniform Random Variate Generation by Luc Devroye. It covers every imaginable distribution and provides hundreds of algorithms.
The subject of random variate generation is also reviewed by Knuth, who describes algorithms for all the major distributions.
The Particle Data Group provides a short review of techniques for generating distributions of random numbers in the “Monte Carlo” section of its Annual Review of Particle Physics.
The Review of Particle Physics is available online in postscript and pdf format.
An overview of methods used to compute cumulative distribution functions can be found in Statistical Computing by W.J. Kennedy and J.E. Gentle. Another general reference is Elements of Statistical Computing by R.A. Thisted.
The cumulative distribution functions for the Gaussian distribution are based on the following papers,
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Autobuild on September, 26 2007 using texi2html 1.78.