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.Y
is the same size asA
, 和randg
generates each element ofY
using a shape parameter equal to the corresponding element ofA
.
y = randg(A,m)
返回m
-by-m
从具有形状参数的伽马分布中选择的随机值矩阵A
.A
is either anm
-by-m
matrix or a scalar. IfA
is a scalar,randg
uses 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
.A
is 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.classname
can 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.X
is a numeric array.
randg
produces pseudo-random numbers using the MATLAB®functionsrand
和randn
. The sequence of numbers generated is determined by the settings of the uniform random number generator that underliesrand
和randn
. Control that shared random number generator usingRNG
. See theRNG
documentation 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 byrand
和randn
to its default startup settings. This wayrandg
produces 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 byrand
和randn
, 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 byrand
和randn
with a seed based on the current time.randg
returns 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.