- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

16.5 Random Matrices

This section describes the basic generators for random matrices in Octave. Additional random distributions can be found in section 24.7 Random Number Generation.

Loadable Function: rand (x)
Loadable Function: rand (n, m)
Loadable Function: rand ("state", x)
Loadable Function: rand ("seed", x)
Return a matrix with random elements uniformly distributed on the interval (0, 1). The arguments are handled the same as the arguments for eye.

You can query the state of the random number generator using the form

v = rand ("state")

This returns a column vector v of length 625. Later, you can restore the random number generator to the state v using the form

rand ("state", v)

You may also initialize the state vector from an arbitrary vector v of length 625 or less. This new state will be a hash based on the value of v, not v itself.

By default, the generator is initialized from /dev/urandom if it is available, otherwise from cpu time, wall clock time and the current fraction of a second.

To compute the pseudo-random sequence, rand uses the Mersenne Twister with a period of 2^{19937-1}.(8) Do not use for cryptography without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.

Older versions of Octave used a different random number generator. The new generator is now used by default as it is faster and produces random numbers with a significantly longer cycle time. However, in some circumstances it might be desirable to obtain the random sequences produced by the old generator. The keyword "seed" specifies that the old generators should be used, as in

rand ("seed", val)

which sets the seed of the generator to val. The seed of the generator can be queried with

s = rand ("seed")

However, it should be noted that querying the seed will not cause rand to use the old generators, only setting the seed will. To cause rand to once again use the new generators, the keyword "state" should be used to reset the state of the rand.

See also randn, rande, randg, randp

Loadable Function: randn (x)
Loadable Function: randn (n, m)
Loadable Function: randn ("state", x)
Loadable Function: randn ("seed", x)
Return a matrix with normally distributed random elements. The arguments are handled the same as the arguments for rand.

By default, randn uses the Marsaglia and Tsang “Ziggurat technique” to transform from a uniform to a normal distribution. (9)

See also rand, rande, randg, randp

Loadable Function: rande (x)
Loadable Function: rande (n, m)
Loadable Function: rande ("state", x)
Loadable Function: rande ("seed", x)
Return a matrix with exponentially distributed random elements. The arguments are handled the same as the arguments for rand.

By default, rande uses the Marsaglia and Tsang Ziggurat technique to transform a uniform distribution to an exponential distribution.

See also rand, randn, randg, randp

Loadable Function: randp (l, x)
Loadable Function: randp (l, n, m)
Loadable Function: randp ("state", x)
Loadable Function: randp ("seed", x)
Return a matrix with Poisson distributed random elements with mean value parameter given by the first argument, l. The arguments are handled the same as the arguments for rand, except for the argument l.

See also rand, randn, rande, randg

Loadable Function: randg (a, x)
Loadable Function: randg (a, n, m)
Loadable Function: randg ("state", x)
Loadable Function: randg ("seed", x)
Return a matrix with gamma(a,1) distributed random elements. The arguments are handled the same as the arguments for rand, except for the argument a.

See also rand, randn, rande, randp

The generators operate in the new or old style together, it is not possible to mix the two. Initializing any generator with "state" or "seed" causes the others to switch to the same style for future calls.

The state of each generator is independent and calls to different generators can be interleaved without affecting the final result. For example,

rand ("state", [11, 22, 33]);
randn ("state", [44, 55, 66]);
u = rand (100, 1);
n = randn (100, 1);

and

rand ("state", [11, 22, 33]);
randn ("state", [44, 55, 66]);
u = zeros (100, 1);
n = zeros (100, 1);
for i = 1:100
  u(i) = rand ();
  n(i) = randn ();
end

produce equivalent results. When the generators are initialized in the old style with "seed" only rand and randn are independent, because the old rande, randg and randp generators make calls to rand and randn.

The generators are initialized with random states at start-up, so that the sequences of random numbers are not the same each time you run Octave.(10) If you really do need to reproduce a sequence of numbers exactly, you can set the state or seed to a specific value.

If invoked without arguments, rand and randn return a single element of a random sequence.

The original rand and randn functions use Fortran code from Ranlib, a library of fortran routines for random number generation, compiled by Barry W. Brown and James Lovato of the Department of Biomathematics at The University of Texas, M.D. Anderson Cancer Center, Houston, TX 77030.

Function File: randperm (n)
Return a row vector containing a random permutation of the integers from 1 to n.

ISBN 095461206XGNU Octave Manual Version 3See the print edition