Computes gamma pseudo-random numbers with a choice of underlying random number generator.


x = rndGamma(r, c, shape, scale)
{ x, newstate } = rndGamma(r, c, shape, scale, state)
  • r (scalar) – number of rows of resulting matrix.
  • c (scalar) – number of columns of resulting matrix.
  • shape (matrix or vector or scalar) – shape argument for gamma distribution, scalar or ExE conformable matrix with r and c.
  • scale (matrix or vector or scalar) – scale argument for gamma distribution, scalar or ExE conformable matrix with r and c.
  • state (scalar or opaque vector) –

    Optional argument.

    scalar case

    state = starting seed value only. If -1, GAUSS computes the starting seed based on the system clock.

    opaque vector case

    state = the state vector returned from a previous call to one of the rnd random number functions.

  • x (RxC matrix) – gamma distributed random numbers.
  • newstate (Opaque vector) – the updated state.


Example 1

num_rows = 5;
num_cols = 1;
shape = 3;
scale = 2;

x = rndGamma(num_rows, num_cols, shape, scale);

Example 2

The gamma distribution is sometimes described in terms of a shape parameter and an inverse scale parameter, called the rate parameter. The rate parameter is the reciprocal of the scale parameter. With this parameterization, the random numbers will have the following properties:

\[ \begin{align}\begin{aligned}\begin{split}E(x) = \frac{shape}{rate}\\\end{split}\\Var(x) = \frac{shape}{rate^2}\end{aligned}\end{align} \]

If you prefer to think about the gamma distribution in these terms, then pass in the reciprocal of the rate parameter as the fourth argument to rndGamma().

shape = 3;
rate = 2;

x = rndGamma(5, 1, shape, 1/rate);


The properties of the pseudo-random numbers in x are:

\[ \begin{align}\begin{aligned}\begin{split}E(x) = shape*scale\\\end{split}\\\begin{split}Var(x) = shape*scale^2\\\end{split}\\\begin{split}x > 0\\\end{split}\\\begin{split}shape > 0\\\end{split}\\scale > 0\end{aligned}\end{align} \]

Technical Notes

The default generator for rndGamma() is the SFMT Mersenne-Twister 19937. You can specify a different underlying random number generator with the function rndCreateState().

See also

Functions rndCreateState(), rndStateSkip()