Distributions of random numbers can be obtained from any of the generators using the functions described in this section. In the simplest cases a non-uniform distribution can be obtained analytically from the uniform distribution with 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 functions described in this section are declared in gsl_randist.h.

The Gaussian Distribution
=========================

 -- Random: double gsl_ran_gaussian (const gsl_rng * R, double SIGMA)
     This function returns a Gaussian random number, with mean zero and standard deviation SIGMA. The probability distribution for Gaussian random numbers is,

          p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx

     for x in the range -\infty to +\infty. Use the transformation z = \mu + x on the numbers returned by gsl_ran_gaussian to obtain a Gaussian distribution with mean \mu.

 -- Function: double gsl_ran_gaussian_pdf (double X, double SIGMA)
     This function computes the probability density p(x) at X for a Gaussian distribution with standard deviation SIGMA, using the formula given above.

 -- Random: double gsl_ran_ugaussian (const gsl_rng * R)
 -- Random: double gsl_ran_ugaussian_pdf (double X)
     These functions compute results for the unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, SIGMA = 1.

Properties
----------

The Gaussian Tail Distribution
==============================

 -- Random: double gsl_ran_gaussian_tail (const gsl_rng * R, double A, double SIGMA)
     This function provides random numbers 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 numbers is,

          p(x) dx = {1 \over N(a;\sigma)} \exp (- x^2/(2 \sigma^2)) dx

     for x > a where N(a;\sigma) is the normalization constant,

          N(a;\sigma) = (1/2) erfc(a / sqrt(2 sigma^2))

 -- Function: double gsl_ran_gaussian_tail_pdf (double X, double A, double SIGMA)
     This function computes the probability density p(x) at X for a Gaussian tail distribution with standard deviation SIGMA and lower limit A, using the formula given above.

 -- Random: double gsl_ran_ugaussian_tail (const gsl_rng * R, double A)
 -- Random: double gsl_ran_ugaussian_tail_pdf (double X, double A)
     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.

The Bivariate Gaussian Distribution
===================================

 -- Random: void gsl_ran_bivariate_gaussian (const gsl_rng * R, double SIGMA_X, double SIGMA_Y, double RHO, double * X, double * Y)
     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 x and y directions. The probability distribution for bivariate gaussian random numbers is,

          p(x,y) dx dy = {1 \over 2 \pi \sigma_x \sigma_y \sqrt{1-\rho^2}} \exp (-(x^2 + y^2 - 2 \rho x y)/2\sigma_x^2\sigma_y^2 (1-\rho^2)) dx dy

     for x,y in the range -\infty to +\infty.  The correlation coefficient RHO should lie between 1 and -1.

 -- Function: double gsl_ran_bivariate_gaussian_pdf (double X, double Y, double SIGMA_X, double SIGMA_Y, double RHO)
     This function computes the probability density p(x,y) at (X,Y) for a bivariate gaussian distribution with standard deviations SIGMA_X, SIGMA_Y and correlation coefficient RHO, using the formula given above.

The Exponential Distribution
============================

 -- Random: double gsl_ran_exponential (const gsl_rng * R, double MU)
     This function returns a random number from the exponential distribution with mean MU.

          p(x) dx = {1 \over \mu} \exp(-x/\mu) dx

     for $x \ge 0$ x >= 0.

 -- Function: double gsl_ran_exponential_pdf (double X, double MU)
     This function computes the probability density p(x) at X for an exponential distribution with mean MU, using the formula given above.

The Laplace Distribution
========================

 -- Random: double gsl_ran_laplace (const gsl_rng * R, double MU)
     This function returns a random number from the the Laplace distribution with width MU. The distribution is,

          p(x) dx = {1 \over 2 \mu}  \exp(-|x/\mu|) dx

     for -\infty < x < \infty.

 -- Function: double gsl_ran_laplace_pdf (double X, double MU)
     This function computes the probability density p(x) at X for a Laplace distribution with mean MU, using the formula given above.

The Exponential Power Distribution
==================================

 -- Random: double gsl_ran_exppow (const gsl_rng * R, double MU, double A)
     This function returns a random number from the exponential power distribution with scale parameter MU and exponent A. The distribution is,

          p(x) dx = {1 \over 2 \mu \Gamma(1+1/a)} \exp(-|x/\mu|^a) dx

     for $x \ge 0$ x >= 0. For a = 1 this reduces to the Laplace distribution. For a = 2 it has the same form as a gaussian distribution, but with $\mu = \sqrt2 \sigma$ \mu = \sqrt{2} \sigma.

 -- Function: double gsl_ran_exppow_pdf (double X, double MU, double A)
     This function computes the probability density p(x) at X for an exponential power distribution with scale parameter MU and exponent A, using the formula given above.

The Cauchy Distribution
=======================

 -- Random: double gsl_ran_cauchy (const gsl_rng * R, double MU)
     This function returns a random number from the Cauchy distribution with scale parameter MU. The probability distribution for Cauchy random numbers is,

          p(x) dx = {1 \over \mu\pi (1 + (x/\mu)^2) } dx

     for x in the range -\infty to +\infty.  The Cauchy distribution is also known as the Lorentz distribution.

 -- Function: double gsl_ran_cauchy_pdf (double X, double MU)
     This function computes the probability density p(x) at X for a Cauchy distribution with scale parameter MU, using the formula given above.

Properties
----------

The Rayleigh Distribution
=========================

 -- Random: double gsl_ran_rayleigh (const gsl_rng * R, double SIGMA)
     This function returns a random number from the Rayleigh distribution with scale parameter SIGMA. The probability distribution for Rayleigh random numbers is,

          p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx

     for x > 0.

 -- Function: double gsl_ran_rayleigh_pdf (double X, double SIGMA)
     This function computes the probability density p(x) at X for an Rayleigh distribution with scale parameter SIGMA, using the formula given above.

Properties
----------

The Rayleigh Tail Distribution
==============================

 -- Random: double gsl_ran_rayleigh_tail (const gsl_rng * R, double A double SIGMA)
     This function returns a random number from the tail of the Rayleigh distribution with scale parameter SIGMA and a lower limit of A. The probability distribution for Rayleigh tail random numbers is,

          p(x) dx = {x \over \sigma^2} \exp ((a^2 - x^2) /(2 \sigma^2)) dx

     for x > a.

 -- Function: double gsl_ran_rayleigh_tail_pdf (double X, double A, double SIGMA)
     This function computes the probability density p(x) at X for an Rayleigh tail distribution with scale parameter SIGMA and lower limit A, using the formula given above.

Properties
----------

The Symmetric Levy Distribution
===============================

 -- Random: double gsl_ran_levy (const gsl_rng * R, double MU, double A)
     This function returns a random number from the symmetric Levy distribution with scale MU and exponent A. The symmetric Levy probability distribution is defined by a fourier transform,

          p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(it(\mu-x)- |t|^a)

     There is no explicit solution for the form of p(x). For a = 1 the distribution reduces to the Cauchy distribution. For a = 2 it is a Gaussian distribution with $\sigma = \sqrt2 \mu$ \sigma = \sqrt{2} \mu. For a < 1 the tails of the distribution become extremely wide.

     The algorithm only works for $0 < a \le 2$ 0 < a <= 2.

 -- Function: double gsl_ran_levy_pdf (double X, double MU)
     This function computes the probability density p(x) at X for a symmetric Levy distribution with scale parameter MU and exponent A, using the formula given above.

Properties
----------

The Gamma Distribution
======================

 -- Random: double gsl_ran_gamma (const gsl_rng * R, double A, double B)
     This function returns a random number from the gamma distribution.  The distribution function is

          p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx

 -- Function: double gsl_ran_gamma_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a gamma distribution with parameters A and B, using the formula given above.

Properties
----------

The Flat (Uniform) Distribution
===============================

 -- Random: double gsl_ran_flat (const gsl_rng * R, double A, double B)
     This function returns a random number from the flat (uniform) distribution from A to B.

          p(x) dx = {1 \over (b-a)} dx

     if $a \le x < b$ a <= x < b and 0 otherwise.

 -- Function: double gsl_ran_flat_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a uniform distribution from A to B, using the formula given above.

Properties
----------

The Lognormal Distribution
==========================

 -- Random: double gsl_ran_lognormal (const gsl_rng * R, double ZETA, double SIGMA)
     This function returns a random number from the lognormal distribution.  The distribution function is

          p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} } \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx

     for x > 0

 -- Function: double gsl_ran_lognormal_pdf (double X, double ZETA, double SIGMA)
     This function computes the probability density p(x) at X for a lognormal distribution with parameters ZETA and SIGMA, using the formula given above.

Properties
----------

The Chi-squared Distribution
============================

The chi-squared distribution arises in statistics If Y_i are n independent gaussian random numbers with unit variance then the sum-of-squares,

     X_i = \sum_i Y_i^2

has a chi-squared distribution with n degrees of freedom.

 -- Random: double gsl_ran_chisq (const gsl_rng * R, double NU)
     This function returns a random number from the chi-squared distribution with NU degrees of freedom.

          p(x) dx = {1 \over \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx

     for $x \ge 0$ x >= 0.

 -- Function: double gsl_ran_chisq_pdf (double X, double NU)
     This function computes the probability density p(x) at X for a chi-squared distribution with NU degrees of freedom, using the formula given above.

Properties
----------

The F-distribution
==================

The F-distribution arises in statistics. If Y_1 and Y_2 are chi-squared deviates with \nu_1 and \nu_2 degrees of freedom then the ratio,

     X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) }

has an F-distribution F(x;\nu_1,\nu_2).

 -- Random: double gsl_ran_fdist (const gsl_rng * R, double NU1, double NU2)
     This function returns a random number from the F-distribution with degrees of freedom NU1 and NU2.

          p(x) dx =
             { \Gamma((\nu_1 + \nu_2)/2)
                  \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) }
             \nu_1^{\nu_1/2} \nu_2^{\nu_2/2}
             x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}

     for $x \ge 0$ x >= 0.

 -- Function: double gsl_ran_fdist_pdf (double X, double NU1, double NU2)
     This function computes the probability density p(x) at X for an F-distribution with NU1 and NU2 degrees of freedom, using the formula given above.

Properties
----------

The t-distribution
==================

The t-distribution arises in statistics. If Y_1 has a normal distribution and Y_2 has a chi-squared distribution with \nu degrees of freedom then the ratio

     X = { Y_1 \over \sqrt{Y_2 / \nu} }

has a t-distribution t(x;\nu) with \nu degrees of freedom.

 -- Random: double gsl_ran_tdist (const gsl_rng * R, double NU)
     This function returns a random number from the t-distribution. The distribution is,

          p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)}
             (1 + x^2/\nu)^{-(\nu + 1)/2} dx

 -- Function: double gsl_ran_tdist_pdf (double X, double NU)
     This function computes the probability density p(x) at X for a t-distribution with NU degrees of freedom, using the formula given above.

The Beta Distribution
=====================

 -- Random: double gsl_ran_beta (const gsl_rng * R, double A, double B)
     This function returns a random number from the beta distribution.  The distribution function is

          p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx

 -- Function: double gsl_ran_beta_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a beta distribution with parameters A and B, using the formula given above.

Properties
----------

The Logistic Distribution
=========================

 -- Random: double gsl_ran_logistic (const gsl_rng * R, double MU)
     This function returns a random number from the logistic distribution.  The distribution function is

          p(x) dx = { \exp(-x/\mu) \over \mu (1 + \exp(-x/\mu))^2 } dx

     for x > 0

 -- Function: double gsl_ran_logistic_pdf (double X, double MU)
     This function computes the probability density p(x) at X for a logistic distribution with scale parameter MU, using the formula given above.

Properties
----------

The Pareto Distribution
=======================

 -- Random: double gsl_ran_pareto (const gsl_rng * R, double A, double B)
     This function returns a random number from the Pareto distribution of order A.  The distribution function is,

          p(x) dx = a b^a / x^{a+1} dx

     for $x \ge b$ x >= b

 -- Function: double gsl_ran_pareto_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a Pareto distribution with exponent A and scale B, using the formula given above.

Properties
----------

The Spherical Distribution (2D & 3D)
====================================

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.

 -- Random: void gsl_ran_dir_2d (const gsl_rng * R, double *X, double *Y)
     This function returns a random direction vector v = (X,Y) in two dimensions. The vector is normalized such that |v|^2 = x^2 + y^2 = 1.  The obvious way to do this is to take a uniform random number between 0 and 2\pi 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 my home Pentium (but not the case for my Sun Sparcstation 20 at work).  Once 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 $\sqrtx^2 + y^2$ \sqrt{x^2 + y^2}. 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 x=(u^2-v^2)/(u^2+v^2) and y=uv/(u^2+v^2).

 -- Random: void gsl_ran_dir_3d (const gsl_rng * R, double *X, double *Y, double * Z)
     This function returns a random direction vector v = (X,Y,Z) in three dimensions. The vector is normalized such that |v|^2 = x^2 + y^2 + z^2 = 1.  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 3d).

 -- Random: void gsl_ran_dir_nd (const gsl_rng * R, int N, double *X)

     This function returns a random direction vector $v = (x_1,x_2,\ldots,x_n)$ v = (x_1,x_2,...,x_n) in N dimensions. The vector is normalized such that $|v|^2 = x_1^2 + x_2^2 + \cdots + x_n^2 = 1$ |v|^2 = x_1^2 + x_2^2 + ... + x_n^2 = 1.  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).

The following program generates a random walk in two dimensions.

     #include <stdio.h>
     #include <gsl_rng.h>
     #include <gsl_randist.h>

     main ()
     {
       gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ;
       int i ;
       double x = 0, y = 0, dx, dy;

       printf("%g %g\n", x, y) ;

       for (i = 0; i < 10; i++)
         {
           gsl_ran_dir_2d (r, &dx, &dy) ;
           x += dx ; y += dy;
           printf("%g %g\n", x, y) ;
         }
     }

Example output from the program, three 10-step random walks from the origin.

The Weibull Distribution
========================

 -- Random: double gsl_ran_weibull (const gsl_rng * R, double MU, double A)
     This function returns a random number from the Weibull distribution. The distribution is,

          p(x) dx = {a \over \mu^a} x^{a-1}  \exp(-(x/\mu)^a) dx

     for -\infty < x < \infty.

 -- Function: double gsl_ran_weibull_pdf (double X, double MU, double A)
     This function computes the probability density p(x) at X for a Weibull distribution with scale MU and exponent A, using the formula given above.

Properties
----------

The Type-1 Gumbel Distribution
==============================

 -- Random: double gsl_ran_gumbel1 (const gsl_rng * R, double A, double B)
     This function returns random numbers from the Type-1 Gumbel distribution. The Type-1 Gumbel distribution is,

          p(x) dx = a b \exp(-(b \exp(-ax) + ax)) dx

     for -\infty < x < \infty.

 -- Function: double gsl_ran_gumbel1_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a Type-1 Gumbel distribution with parameters A and B, using the formula given above.

Properties
----------

The Type-2 Gumbel Distribution
==============================

 -- Random: double gsl_ran_gumbel2 (const gsl_rng * R, double A, double B)
     This function returns random numbers from the Type-2 Gumbel distribution. The Type-2 Gumbel distribution is,

          p(x) dx = a b x^{-a-1} \exp(-b x^{-a}) dx

     for 0 < x < \infty.

 -- Function: double gsl_ran_gumbel2_pdf (double X, double A, double B)
     This function computes the probability density p(x) at X for a Type-2 Gumbel distribution with parameters A and B, using the formula given above.

Properties
----------

General Discrete Distributions
==============================

Given K discrete events with different probabilities P[K], produce a random value K consistent with its probability.

The obvious way to do this is to preprocess the probability list by generating a cumulative probability array with K+1 elements:

       C[0] = 0
     C[k+1] = C[k]+P[k].

Note that this construction produces C[K]=1.  Now choose a uniform deviate U between 0 and 1, and find the value of K such that $C[k] \le u < C[k+1]$ C[k] <= u < C[k+1]. Although this in principle requires of order \log K steps per random number generation, they are fast steps, and if you use something like \lfloor uK \rfloor 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 K, 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 K.  After preprocessing, the random numbers are generated in O(1) time, even for large K.  The preprocessing suggested by Walker requires O(K^2) effort, but that is not actually necessary, and the implementation provided here only takes O(K) 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 K.

This method can be used to speed up some of the discrete random number generators below, such as the binomial distribution.  To use if for something like the Poisson Distribution, a modification would have to be made, since it only takes a finite set of K outcomes.

 -- Random: gsl_ran_discrete_t * gsl_ran_discrete_preproc (size_t K, const double * P)
     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.

 -- Random: size_t gsl_ran_discrete (const gsl_rng * R, const gsl_ran_discrete_t * G)
     After the preprocessor, above, has been called, you use this function to get the discrete random numbers.

 -- Random: double gsl_ran_discrete_pdf (size_t K, const gsl_ran_discrete_t * G)
     Returns the probability P[K] of observing the variable K. Since P[K] is not stored as part of the lookup table, it must be recomputed; this computation takes O(K), so if K is large and you care about the original array P[K] used to create the lookup table, then you should just keep this original array P[K] around.

 -- Random: void gsl_ran_discrete_free (gsl_ran_discrete_t * G)
     De-allocates the lookup table pointed to by G.

The Poisson Distribution
========================

 -- Random: unsigned int gsl_ran_poisson (const gsl_rng * R, double MU)
     This function returns a random integer from the Poisson distribution with mean MU. The probability distribution for Poisson random numbers is,

          p(k) = {\mu^k \over k!} \exp(-\mu)

     for $k \ge 0$ k >= 0.

 -- Function: double gsl_ran_poisson_pdf (unsigned int K, double MU)
     This function computes the probability p(k) of obtaining  K from a Poisson distribution with mean MU, using the formula given above.

Properties
----------

The Bernoulli Distribution
==========================

 -- Random: unsigned int gsl_ran_bernoulli (const gsl_rng * R, double P)
     This function returns the result either 0 or 1, the result of a Bernoulli trial with probability P. The probability distribution for a Bernoulli trial is,

          p(0) = 1 - p
          p(1) = p

 -- Function: double gsl_ran_bernoulli_pdf (unsigned int K, double P)
     This function computes the probability p(k) of obtaining K from a Bernoulli distribution with probability parameter P, using the formula given above.

Properties
----------

The Binomial Distribution
=========================

 -- Random: unsigned int gsl_ran_binomial (const gsl_rng * R, double P, unsigned int N)
     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 random numbers is,

          p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k}

     for $0 \le k \le n$ 0 <= k <= n.

 -- Function: double gsl_ran_binomial_pdf (unsigned int K, double P, unsigned int N)
     This function computes the probability p(k) of obtaining K from a binomial distribution with parameters P and N, using the formula given above.

Properties
----------

The Negative Binomial Distribution
==================================

 -- Random: unsigned int gsl_ran_negative_binomial (const gsl_rng * R, double P, double N)
     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 random numbers is,

          p(k) = {\Gamma(n + k) \over \Gamma(k+1) \Gamma(n) } p^n (1-p)^k

     Note that k is not required to be an integer.

 -- Function: double gsl_ran_nbinomial_pdf (unsigned int K, double P, double N)
     This function computes the probability p(k) of obtaining K from a negative binomial distribution with parameters P and N, using the formula given above.

 -- Random: unsigned int gsl_ran_pascal (const gsl_rng * R, double P, unsigned int K)
     This function returns a random integer from the Pascal distribution. The Pascal distribution is simply a negative binomial distribution with an integer value of n.

          p(k) = {(n + k - 1)! \over k! (n -1)! } p^n (1-p)^k

 -- Function: double gsl_ran_pascal_pdf (unsigned int K, double P, unsigned int N)
     This function computes the probability p(k) of obtaining K from a Pascal distribution with parameters P and N, using the formula given above.

Properties
----------

The Geometric Distribution
==========================

 -- Random: unsigned int gsl_ran_geometric (const gsl_rng * R, double P)
     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 random numbers is,

          p(k) =  p (1-p)^k

     for $k \ge 1$ k >= 1.

 -- Function: double gsl_ran_geometric_pdf (unsigned int K, double P)
     This function computes the probability p(k) of obtaining K from a geometric distribution with probability parameter P, using the formula given above.

Properties
----------

The Hypergeometric Distribution
===============================

 -- Random: unsigned int gsl_ran_hypergeometric (const gsl_rng * R, unsigned int N1, unsigned int N2, unsigned int T)
     This function returns a random integer from the hypergeometric distribution. The probability distribution for hypergeometric random numbers is,

          p(k) =  C(n_1,k) C(n_2, t-k) / C(n_1 + n_2,k)

     where C(a,b) = a!/(b!(a-b)!). The domain of k is $\hboxmax(0\,t-n_2), \ldots, \hboxmax(t,n_1)$ max(0,t-n_2), ..., max(t,n_1).

 -- Function: double gsl_ran_hypergeometric_pdf (unsigned int K, unsigned int N1, unsigned int N2, unsigned int T)
     This function computes the probability p(k) of obtaining K from a hypergeometric distribution with parameters N1, N2, N3, using the formula given above.

Properties
----------

The Logarithmic Distribution
============================

 -- Random: unsigned int gsl_ran_logarithmic (const gsl_rng * R, double P)
     This function returns a random integer from the logarithmic distribution. The probability distribution for logarithmic random numbers is,

          p(k) = {-1 \over \log(1-p)} {(p^k \over k)}

     for $k \ge 1$ k >= 1.

 -- Function: double gsl_ran_logarithmic_pdf (unsigned int K, double P)
     This function computes the probability p(k) of obtaining K from a logarithmic distribution with probability parameter P, using the formula given above.

Properties
----------

Shuffling and Sampling
======================

 -- Random: void gsl_ran_shuffle (const gsl_rng * R, void * BASE, size_t N, size_t SIZE)

     This function randomly shuffles the order of N objects, each of size SIZE, stored in the array BASE[0..N-1].

     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));

 -- Random: void * gsl_ran_choose (const gsl_rng * R, void * DEST, size_t K, void * SRC, size_t N, size_t SIZE)
     This function fills the array DEST[K] with K objects taken randomly from the N elements of the array SRC[N]. The objects are each of size SIZE. 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));

Examples
========

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.

     #include <stdio.h>
     #include <gsl_rng.h>
     #include <gsl_randist.h>

     int
     main ()
     {
       gsl_rng * r ;

       int i, n = 10;
       double mu = 3.0;

       /* create a generator chosen by the environment variable GSL_RNG_TYPE */

       gsl_rng_env_setup();

       r = gsl_rng_alloc (gsl_rng_default);

       /* print n random variates chosen from the poisson distribution with
          mean parameter mu */

       for (i = 0; i < n; i++)
         {
           unsigned int k = gsl_ran_poisson (r, mu);

           printf(" %u", k);
         }

       printf("\n");
     }

If the library and header files are installed under /usr/local (the default location) then the program can be compiled with these options,

     gcc -I/usr/local/include/gsl -L/usr/local/lib/gsl demo.c -lgslrandist
       -lgslrng -lgslspecfunc -lgslerr -lm

Here is the output of the program,

     $ ./a.out
      4 2 3 3 1 3 4 1 3 5

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.out
     GSL_RNG_SEED=123
      1 1 2 1 2 6 2 1 8 7

References and Further Reading
==============================

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.

  Luc Devroye, Non-Uniform Random Variate Generation, Springer-Verlag, ISBN 0-387-96305-7.

The subject of random variate generation is also reviewed by Knuth, who describes algorithms for all the major distributions.

  Donald E. Knuth, The Art of Computer Programming: Seminumerical Algorithms (Vol 2, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896842.

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.

  Review of Particle Properties R.M. Barnett et al., Physical Review D54, 1 (1996) http://pdg.lbl.gov/.

The Review of Particle Physics is available online in postscript and pdf format.