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
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 as
A
, and randg
generates each element of
Y
using a shape parameter equal to the corresponding element of
A
.
Y = randg(A,m)
returns an
m
-by-m
matrix of random values chosen from
gamma distributions with shape parameters A
. A
is
either an m
-by-m
matrix or a scalar. If
A
is a scalar, randg
uses that single shape
parameter value to generate all elements of Y
.
Y = randg(A,m,n,p,...)
or
Y =
randg(A,[m,n,p,...])
returns an
m
-by-n
-by-p
-by-...
array of random values chosen from gamma distributions with shape parameters
A
. A
is either an
m
-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 be
double
or single
.
Y = randg(...,'like',X)
or
Y = randg(...,'like',classname)
returns an array of random values
chosen from gamma distributions of the same class as X
or
classname
, respectively. X
is a numeric
array.
randg
produces pseudo-random numbers using the MATLAB® functions rand
and randn
. The
sequence of numbers generated is determined by the settings of the uniform random number
generator that underlies rand
and randn
. Control
that shared random number generator using rng
. See the rng
documentation for more information.
Note
To generate gamma random numbers and specify both the scale and shape
parameters, you should call gamrnd
.
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
Generate a 100-by-2 array of values drawn from 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 from randg
, reset the random
number generator used by rand
and randn
to its
default startup settings. This way randg
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 by rand
and randn
, generate 5 values from randg
,
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 as
r1
.
Example 5
Reinitialize the random number generator used by rand
and
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);
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
Version History
Introduced before R2006a