- publishing free software manuals
 GNU Octave Manual Version 3 by John W. Eaton, David Bateman, Søren HaubergPaperback (6"x9"), 568 pagesISBN 095461206XRRP £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.

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`.

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)

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.

Loadable Function: randp (l, n, m)
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.

Loadable Function: randg (a, n, m)
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.

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