rand

Generate uniform random values on the interval (0,1).

Syntax

r = rand()

r = rand(n)

r = rand(m,n,...)

r = rand([m,n,...])

rand('seed',seed)

v = rand('state')

rand('state',v)

Inputs

m, n, ...
The length of each dimension in the output matrix.
Type: integer
Dimension: scalar
seed
A seed value to initialize the random number generator.
Type: nonnegative integer
Dimension: scalar
v
A vector containing the state of the random number generator.
Type: integer
Dimension: vector

Outputs

r
Uniform random values.
v
A vector containing the state of the random number generator.
Type: integer
Dimension: vector

Examples

Square matrix example:
rand('seed',0);
r = rand(3)
r = [Matrix] 3 x 3
0.54881  0.84427  0.54488
0.59284  0.60276  0.84725
0.71519  0.85795  0.42365
Rectangular matrix example:
rand('seed',0);
r = rand(2,4)
r = [Matrix] 2 x 4
0.54881  0.71519  0.60276  0.54488
0.59284  0.84427  0.85795  0.84725

Comments

rand() generates scalar outputs.

rand(n) generates a square matrix.

The random number generator algorithm is the Mersenne Twister.

The seed value initializes the generator state and has no post-initialization significance. It does not update as the generator is used, as happens in some generators. If the seed is not set, it defaults to 0 the first time that the generator is used.

The state vector can be accessed and restored later if more than one random sequence is desired. For example:

        rand('seed', 2017);    % initialize with seed
        v = rand('state');     % save state vector
        rand('seed', 2018);    % initialize with new seed
        rand('state', v);      % restore saved state

Only create state vectors by setting seed values.