Main Content

gamrnd

Gamma random numbers

Description

example

r = gamrnd(a,b) generates a random number from the gamma distribution with the shape parameter a and the scale parameter b.

example

r = gamrnd(a,b,sz1,...,szN) generates an array of random numbers from the gamma distribution, where sz1,...,szN indicates the size of each dimension.

example

r = gamrnd(a,b,sz) generates an array of random numbers from the gamma distribution, where vector sz specifies size(r).

Examples

collapse all

Generate a single random number from the gamma distribution with shape 5 and scale 7.

r = gamrnd(5,7)
r = 68.9857

Generate five random numbers from the gamma distributions with shape parameter values 1 through 5 and scale parameter 2.

a1 = 1:5;
b1 = 2;
r1 = gamrnd(a1,b1)
r1 = 1×5

    7.1297    6.0918    2.1010    8.7253   29.5447

By default, gamrnd generates an array that is the same size as a and b after any necessary scalar expansion so that all scalars are expanded to match the dimensions of the other inputs.

If you specify array dimensions sz1,...,szN or sz, they must match the dimensions of a and b after any necessary scalar expansion.

Generate a 2-by-3 array of random numbers from the gamma distribution with shape parameter 3 and scale parameter 7.

sz = [2 3];
r2 = gamrnd(3,7,sz)
r2 = 2×3

   17.9551   41.3983    7.9865
   16.4204   40.0048   44.1909

Generate six random numbers from the gamma distributions with shape parameter values 1 through 6 and scale parameter values 5 through 10 respectively.

a3 = 1:6;
b3 = 5:10;
r3 = gamrnd(a3,b3,1,6)
r3 = 1×6

    9.5930    7.8289   11.0360   15.0367   28.1456   98.2664

Input Arguments

collapse all

Shape parameter of the gamma distribution, specified as a nonnegative scalar value or an array of nonnegative scalar values.

To generate random numbers from multiple distributions, specify a and b using arrays. If both a and b are arrays, then the array sizes must be the same. If either a or b is a scalar, then gamrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in a and b.

Example: [3 4 7 9]

Data Types: single | double

Scale parameter of the gamma distribution, specified as a nonnegative scalar value or an array of nonnegative scalar values.

To generate random numbers from multiple distributions, specify a and b using arrays. If both a and b are arrays, then the array sizes must be the same. If either a or b is a scalar, then gamrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in a and b.

Example: [1 1 2 2]

Data Types: single | double

Size of each dimension, specified as separate arguments of integers.

If either a or b is an array, then the specified dimensions sz1,...,szN must match the common dimensions of a and b after any necessary scalar expansion. The default values of sz1,...,szN are the common dimensions.

  • If you specify a single value sz1, then r is a square matrix of size sz1-by-sz1.

  • If the size of any dimension is 0 or negative, then r is an empty array.

  • Beyond the second dimension, gamrnd ignores trailing dimensions with a size of 1. For example, gamrnd(2,5,3,1,1,1) produces a 3-by-1 vector of random numbers from the gamma distribution with shape 2 and scale 5.

Example: 2,4

Data Types: single | double

Size of each dimension, specified as a row vector of integers.

If either a or b is an array, then the specified dimensions sz must match the common dimensions of a and b after any necessary scalar expansion. The default values of sz are the common dimensions.

  • If you specify a single value [sz1], then r is a square matrix of size sz1-by-sz1.

  • If the size of any dimension is 0 or negative, then r is an empty array.

  • Beyond the second dimension, gamrnd ignores trailing dimensions with a size of 1. For example, gamrnd(2,5,[3 1 1 1]) produces a 3-by-1 vector of random numbers from the gamma distribution with shape 2 and scale 5.

Example: [2 4]

Data Types: single | double

Output Arguments

collapse all

Gamma random numbers, returned as a nonnegative scalar value or an array of nonnegative scalar values with the dimensions specified by sz1,...,szN or sz. Each element in r is the random number generated from the distribution specified by the corresponding elements in a and b.

Alternative Functionality

  • gamrnd is a function specific to the gamma distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To use random, create a GammaDistribution probability distribution object and pass the object as an input argument or specify the probability distribution name and its parameters. Note that the distribution-specific function gamrnd is faster than the generic function random.

  • Use randg to generate random numbers from the standard gamma distribution (unit scale).

  • To generate random numbers interactively, use randtool, a user interface for random number generation.

References

[1] Marsaglia, George, and Wai Wan Tsang. “A Simple Method for Generating Gamma Variables.” ACM Transactions on Mathematical Software 26, no. 3 (September 1, 2000): 363–72. https://doi.org/10.1145/358407.358414.

Extended Capabilities

Version History

Introduced before R2006a