rand

Uniformly distributed pseudorandom numbers

Syntax

  • X = rand(classname)
  • X = rand(n,classname)
  • X = rand(sz1,...,szN,classname) example
  • X = rand(sz,classname)
  • X = rand('like',p)
  • X = rand(n,'like',p)
  • X = rand(sz1,...,szN,'like',p)
  • X = rand(sz,'like',p) example

Description

example

X = rand returns a pseudorandom scalar drawn from the standard uniform distribution on the open interval (0,1).

example

X = rand(n) returns an n-by-n matrix of pseudorandom uniform values.

example

X = rand(sz1,...,szN) returns an sz1-by-...-by-szN array where sz1,...,szN indicates the size of each dimension. For example, rand(3,4) returns a 3-by-4 array of pseudorandom values.

example

X = rand(sz) returns an array where size vector sz defines size(X). For example, rand([3,4]) returns a 3-by-4 array of pseudorandom values.

X = rand(classname) returns a pseudorandom uniform value where classname specifies the data type. classname can be either 'single' or 'double'.

X = rand(n,classname) returns an n-by-n array of data type classname.

example

X = rand(sz1,...,szN,classname) returns an sz1-by-...-by-szN array of data type classname.

X = rand(sz,classname) returns an array where size vector sz defines size(X) and classname defines class(X).

X = rand('like',p) returns a pseudorandom uniform value like p; that is, with the same data type (class).

X = rand(n,'like',p) returns an n-by-n array like p.

X = rand(sz1,...,szN,'like',p) returns an sz1-by-...-by-szN array like p.

example

X = rand(sz,'like',p) returns an array like p where size vector sz defines size(X).

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

Examples

expand all

Square Matrix of Random Numbers

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

r = rand(5)
r =

    0.5468    0.6791    0.8852    0.3354    0.6538
    0.5211    0.3955    0.9133    0.6797    0.4942
    0.2316    0.3674    0.7962    0.1366    0.7791
    0.4889    0.9880    0.0987    0.7212    0.7150
    0.6241    0.0377    0.2619    0.1068    0.9037

Random Numbers Within Specified Interval

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);.

Random Integers

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

Random Complex Numbers

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

Control Random Number Generation

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.0975    0.2785    0.5469    0.9575    0.9649

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.0975    0.2785    0.5469    0.9575    0.9649

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.

3-D Array of Random Numbers

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

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

    0.8909    0.1978
    0.3342    0.0305
    0.6987    0.7441


X(:,:,2) =

    0.5000    0.6099
    0.4799    0.6177
    0.9047    0.8594


X(:,:,3) =

    0.8055    0.2399
    0.5767    0.8865
    0.1829    0.0287

Random Numbers of Other Data Types

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

r = rand(1,4,'single')
r =

    0.1270    0.9134    0.6324    0.0975
class(r)
ans =

single

Size Defined by Existing Array

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

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

    0.4899    0.9787
    0.1679    0.7127

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

X = rand(size(A));

Size and Numeric Data Type Defined by Existing Array

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 =

    0.5005    0.0596
    0.4711    0.6820
class(X)
ans =

single

Input Arguments

expand all

n — Size of square matrixinteger value

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

sz1,...,szN — Size of each dimension (as separate arguments)integer values

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

sz — Size of each dimension (as a row vector)integer values

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.

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

classname — Data type (class) to create'double' (default) | 'single'

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

Example: rand(5,'single')

Data Types: char

p — Prototype of array to createnumeric array

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

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

Data Types: single | double
Complex Number Support: Yes

Was this topic helpful?