rand

Uniformly distributed random numbers

Syntax

X = rand
X = rand(n)
X = rand(sz1,...,szN)
X = rand(sz)
X = rand(___,typename)
X = rand(___,'like',p)

Description

example

X = rand returns a single uniformly distributed random number in the interval (0,1).

example

X = rand(n) returns an n-by-n matrix of random numbers.

example

X = rand(sz1,...,szN) returns an sz1-by-...-by-szN array of random numbers where sz1,...,szN indicate the size of each dimension. For example, rand(3,4) returns a 3-by-4 matrix.

example

X = rand(sz) returns an array of random numbers where size vector sz specifies size(X). For example, rand([3 4]) returns a 3-by-4 matrix.

example

X = rand(___,typename) returns an array of random numbers of data type typename. The typename input can be either 'single' or 'double'. You can use any of the input arguments in the previous syntaxes.

example

X = rand(___,'like',p) returns an array of random numbers like p; that is, of the same object type as p. You can specify either typename or 'like', but not both.

Note

The 'seed', 'state', and 'twister' inputs to the rand function are not recommended. Use the rng function instead. For more information, see Replace Discouraged Syntaxes of rand and randn.

Examples

collapse all

Generate a 5-by-5 matrix of uniformly distributed random numbers between 0 and 1.

r = rand(5)
r = 

    0.8147    0.0975    0.1576    0.1419    0.6557
    0.9058    0.2785    0.9706    0.4218    0.0357
    0.1270    0.5469    0.9572    0.9157    0.8491
    0.9134    0.9575    0.4854    0.7922    0.9340
    0.6324    0.9649    0.8003    0.9595    0.6787

Generate a 10-by-1 column vector of uniformly distributed numbers in the interval (-5,5).

r = -5 + (5+5)*rand(10,1)
r = 

    3.1472
    4.0579
   -3.7301
    4.1338
    1.3236
   -4.0246
   -2.2150
    0.4688
    4.5751
    4.6489

In general, you can generate N random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(N,1).

Use the randi function (instead of rand) to generate 5 random integers from the uniform distribution between 10 and 50.

r = randi([10 50],1,5)
r = 

    43    47    15    47    35

Generate a single random complex number with real and imaginary parts in the interval (0,1).

a = rand + 1i*rand
a = 
   0.8147 + 0.9058i

Save the current state of the random number generator and create a 1-by-5 vector of random numbers.

s = rng;
r = rand(1,5)
r = 

    0.8147    0.9058    0.1270    0.9134    0.6324

Restore the state of the random number generator to s, and then create a new 1-by-5 vector of random numbers. The values are the same as before.

rng(s);
r1 = rand(1,5)
r1 = 

    0.8147    0.9058    0.1270    0.9134    0.6324

Always use the rng function (rather than the rand or randn functions) to specify the settings of the random number generator. For more information, see Replace Discouraged Syntaxes of rand and randn.

Create a 3-by-2-by-3 array of random numbers.

X = rand([3,2,3])
X = 
X(:,:,1) =

    0.8147    0.9134
    0.9058    0.6324
    0.1270    0.0975


X(:,:,2) =

    0.2785    0.9649
    0.5469    0.1576
    0.9575    0.9706


X(:,:,3) =

    0.9572    0.1419
    0.4854    0.4218
    0.8003    0.9157

Create a 1-by-4 vector of random numbers whose elements are single precision.

r = rand(1,4,'single')
r = 1x4 single row vector

    0.8147    0.9058    0.1270    0.9134

class(r)
ans = 
'single'

Create a matrix of random numbers with the same size as an existing array.

A = [3 2; -2 1];
sz = size(A);
X = rand(sz)
X = 

    0.8147    0.1270
    0.9058    0.9134

It is a common pattern to combine the previous two lines of code into a single line:

X = rand(size(A));

Create a 2-by-2 matrix of single precision random numbers.

p = single([3 2; -2 1]);

Create an array of random numbers that is the same size and data type as p.

X = rand(size(p),'like',p)
X = 2x2 single matrix

    0.8147    0.1270
    0.9058    0.9134

class(X)
ans = 
'single'

If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of random numbers with underlying data type single. For the distributed data type, the 'like' syntax clones the underlying data type in addition to the primary data type.

p = rand(1000,'single','distributed');
Starting parallel pool (parpool) using the 'local' profile ...
connected to 6 workers.

Create an array of random numbers that is the same size, primary data type, and underlying data type as p.

X = rand(size(p),'like',p);
class(X)
ans =

distributed
classUnderlying(X)
ans =
single

Input Arguments

collapse all

Size of square matrix, specified as an integer value.

  • If n is 0, then X is an empty matrix.

  • If n is negative, then it is treated as 0.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension, specified as separate arguments of integer values.

  • If the size of any dimension is 0, then X is an empty array.

  • If the size of any dimension is negative, then it is treated as 0.

  • Beyond the second dimension, rand ignores trailing dimensions with a size of 1. For example, rand(3,1,1,1) produces a 3-by-1 vector of random numbers.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:

  • If the size of any dimension is 0, then X is an empty array.

  • If the size of any dimension is negative, then it is treated as 0.

  • Beyond the second dimension, rand ignores trailing dimensions with a size of 1. For example, rand([3,1,1,1]) produces a 3-by-1 vector of random numbers.

Example: sz = [2,3,4] creates a 2-by-3-by-4 array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Data type (class) to create, specified as 'double', 'single', or the name of another class that provides rand support.

Example: rand(5,'single')

Prototype of array to create, specified as a numeric array.

Example: rand(5,'like',p)

Data Types: single | double
Complex Number Support: Yes

Tips

  • The sequence of numbers produced by rand is determined by the internal settings of the uniform pseudorandom number generator that underlies rand, randi, and randn. You can control that shared random number generator using rng.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?