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

## 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 formrand ("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 withs = 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(`

distributed random elements. The arguments are handled the same as the arguments for`a`,1)`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 095461206X | GNU Octave Manual Version 3 | See the print edition |