MATLAB® uses algorithms to generate pseudorandom and pseudoindependent numbers. These numbers are not strictly random and independent in the mathematical sense, but they pass various statistical tests of randomness and independence, and their calculation can be repeated for testing or diagnostic purposes.
The rand
, randi
, randn
, and randperm
functions are the primary functions for creating arrays of
random numbers. The rng
function allows you to control the
seed and algorithm that generates random numbers.
There are four fundamental random number functions: rand
, randi
, randn
, and randperm
. The
rand
function returns real numbers between 0 and 1 that are
drawn from a uniform distribution. For example,
r1 = rand(1000,1);
r1
is a 1000-by-1 column vector containing real floating-point numbers drawn from a
uniform distribution. All the values in r1
are in the open
interval (0, 1). A histogram of these values is roughly flat, which indicates a
fairly uniform sampling of numbers.The randi
function returns double
integer
values drawn from a discrete uniform distribution. For
example,
r2 = randi(10,1000,1);
r2
is a 1000-by-1 column vector containing integer values drawn from a discrete uniform
distribution whose range is 1,2,...,10. A histogram of these values is roughly flat,
which indicates a fairly uniform sampling of integers between 1 and 10. The randn
function returns arrays of real floating-point
numbers that are drawn from a standard normal distribution. For
example,
r3 = randn(1000,1);
r3
is a 1000-by-1 column vector containing numbers drawn from a standard normal
distribution. A histogram of r3
looks like a roughly normal
distribution whose mean is 0 and standard deviation is 1.You can use the randperm
function to create arrays of random
integer values that have no repeated values. For
example,
r4 = randperm(15,5);
r4
is a 1-by-5 array containing randomly selected integer values on the closed
interval, [1, 15]. Unlike randi
, which can return an array
containing repeated values, the array returned by randperm
has
no repeated values.Successive calls to any of these functions return different results. This behavior is useful for creating several different arrays of random values.
MATLAB offers several generator algorithm options, which are summarized in the following table.
Keyword | Generator | Multiple Stream and Substream Support | Approximate Period In Full Precision |
---|---|---|---|
mt19937ar | Mersenne twister (used by default stream at MATLAB startup) | No | 219937-1 |
dsfmt19937 | SIMD-oriented fast Mersenne twister | No | 219937-1 |
mcg16807 | Multiplicative congruential generator | No | 231-2 |
mlfg6331_64 | Multiplicative lagged Fibonacci generator | Yes | 2124 (251 streams of length 272) |
mrg32k3a | Combined multiple recursive generator | Yes | 2191 (263 streams of length 2127) |
philox4x32_10 | Philox 4x32 generator with 10 rounds | Yes | 2193 (264 streams of length 2129) |
threefry4x64_20 | Threefry 4x64 generator with 20 rounds | Yes | 2514 (2256 streams of length 2258) |
shr3cong | Shift-register generator summed with linear congruential generator | No | 264 |
swb2712 | Modified subtract with borrow generator | No | 21492 |
Use the rng
function to set the seed and
generator used by the rand
, randi
,
randn
, and randperm
functions. For
example, rng('shuffle','philox')
seeds the Philox 4x32 generator
based on the current time, producing a different sequence of numbers each time it is
called.
For more information, see Controlling Random Number Generation.
rand
| randi
| randn
| randperm
| rng