Main Content

randg

Gamma random numbers with unit scale

Syntax

Y = randg
Y = randg(A)
Y = randg(A,m)
Y = randg(A,m,n,p,...)
Y = randg(A,[m,n,p,...])
Y = randg(...,classname)
Y = randg(...,'like',X)
Y = randg(...,'like',classname)

Description

Y = randgreturns a scalar random value chosen from a gamma distribution with unit scale and shape.

Y = randg(A)returns a matrix of random values chosen from gamma distributions with unit scale.Yis the same size asA, andrandggenerates each element ofYusing a shape parameter equal to the corresponding element ofA.

Y = randg(A,m)returns anm-by-mmatrix of random values chosen from gamma distributions with shape parametersA.Ais either anm-by-mmatrix or a scalar. IfAis a scalar,randguses that single shape parameter value to generate all elements ofY.

Y = randg(A,m,n,p,...)orY = randg(A,[m,n,p,...])returns anm-by-n-by-p-by-...array of random values chosen from gamma distributions with shape parametersA.Ais either anm-by-n-by-p-by-...array or a scalar.

Y = randg(...,classname)returns an array of random values chosen from gamma distributions of the specified class.classnamecan bedoubleorsingle.

Y = randg(...,'like',X)orY = randg(...,'like',classname)returns an array of random values chosen from gamma distributions of the same class asXorclassname, respectively.Xis a numeric array.

randgproduces pseudo-random numbers using the MATLAB®functionsrandandrandn. The sequence of numbers generated is determined by the settings of the uniform random number generator that underliesrandandrandn. Control that shared random number generator usingrng. See therngdocumentation for more information.

Note

To generate gamma random numbers and specify both the scale and shape parameters, you should callgamrnd.

Examples

Example 1

Generate a 100-by-1 array of values drawn from a gamma distribution with shape parameter 3.

r = randg(3,100,1);

Example 2

生成一个100 -从- 2数组的值gamma distributions with shape parameters 3 and 2.

A = [ones(100,1)*3,ones(100,1)*2]; r = randg(A,[100,2]);

Example 3

To create reproducible output fromrandg, reset the random number generator used byrandandrandnto its default startup settings. This wayrandgproduces the same random numbers as if you restarted MATLAB.

rng('default') randg(3,1,5) ans = 6.9223 4.3369 1.0505 3.2662 11.3269

Example 4

保存设置随机数生成器used byrandandrandn, generate 5 values fromrandg, restore the settings, and repeat those values.

s = rng; % Obtain the current state of the random stream r1 = randg(10,1,5) r1 = 9.4719 9.0433 15.0774 14.7763 6.3775 rng(s); % Reset the stream to the previous state r2 = randg(10,1,5) r2 = 9.4719 9.0433 15.0774 14.7763 6.3775

r2contains exactly the same values asr1.

Example 5

Reinitialize the random number generator used byrandandrandnwith a seed based on the current time.randgreturns different values each time you do this. Note that it is usually not necessary to do this more than once per MATLAB session.

rng('shuffle'); randg(2,1,5);

References

[1] Marsaglia, G., and W. W. Tsang. “A Simple Method for Generating Gamma Variables.”ACM Transactions on Mathematical Software.Vol. 26, 2000, pp. 363–372.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

Introduced before R2006a