主要内容

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 = randg返回从具有单位尺度和形状的伽马分布中选择的标量随机值。

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

y = randg(A,m)返回m-by-m从具有形状参数的伽马分布中选择的随机值矩阵A.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,...)或者y = randg(A,[m,n,p,...])返回m-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)返回array of random values chosen from gamma distributions of the specified class.classnamecan bedouble或者single.

y = randg(...,'like',x)或者y = randg(...,'like',classname)返回array of random values chosen from gamma distributions of the same class asX或者classname, respectively.Xis a numeric array.

randgproduces pseudo-random numbers using the MATLAB®functionsrandrandn. The sequence of numbers generated is determined by the settings of the uniform random number generator that underliesrandrandn. 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 byrandrandnto 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

Save the settings for the random number generator used byrandrandn, generate 5 values fromrandg,还原设置,然后重复这些值。

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

r2包含与R1.

Example 5

Reinitialize the random number generator used byrandrandnwith 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);

参考

[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™.

Version History

在R2006a之前引入

See Also