Gamma random numbers with unit scale
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)
Y = randg
returns 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.Y
is the same size asA
, andrandg
generates each element ofY
using a shape parameter equal to the corresponding element ofA
.
Y = randg(A,m)
returns anm
-by-m
matrix of random values chosen from gamma distributions with shape parametersA
.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,...)
orY = randg(A,[m,n,p,...])
returns anm
-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)
returns an array of random values chosen from gamma distributions of the specified class.classname
can bedouble
orsingle
.
Y = randg(...,'like',X)
orY = randg(...,'like',classname)
returns an array of random values chosen from gamma distributions of the same class asX
orclassname
, respectively.X
is a numeric array.
randg
produces pseudo-random numbers using the MATLAB®functionsrand
andrandn
. The sequence of numbers generated is determined by the settings of the uniform random number generator that underliesrand
andrandn
. 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
.
Generate a 100-by-1 array of values drawn from a gamma distribution with shape parameter 3.
r = randg(3,100,1);
生成一个100 -从- 2数组的值gamma distributions with shape parameters 3 and 2.
A = [ones(100,1)*3,ones(100,1)*2]; r = randg(A,[100,2]);
To create reproducible output fromrandg
, reset the random number generator used byrand
andrandn
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
保存设置随机数生成器used byrand
andrandn
, 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
r2
contains exactly the same values asr1
.
Reinitialize the random number generator used byrand
andrandn
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.