rand

Uniformly distributed random numbers in the open interval (0, 1).

Category: Language Fundamentals > Matrices and Arrays

Syntax

r = rand

A single random scalar.

r = rand(n)

n-by-n matrix of random numbers.

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

Array of the given size.

r = rand(___, classname)

'double' (default) or 'single'.

Description

The numbers come from a pseudorandom generator whose state advances with every call, so two calls give different values. To get the same sequence again, reset the generator with rng: after rng(1), the next rand(1, 3) is always 0.4170 0.7203 0.0001. Fix the seed in any example, test or experiment that must be reproducible.

To scale to an interval, use a + (b-a)*rand. For integers use randi, and for normally distributed values use randn.

Input arguments

n, m, ...
Dimension sizes.
classname
Class of the result.

Output arguments

r
Array of values strictly between 0 and 1.

Example

A seeded draw

rng(1)
rand(1, 3)
rng(1)
size(rand(2, 3, 4))
Output
ans = 1×3

    0.4170    0.7203    0.0001
ans = 1×3

     2     3     4

See also

randn randi randperm rng