Documentation

## Create Arrays of Random Numbers

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.

### Random Number Functions

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.

### Random Number Generators

MATLAB offers several generator algorithm options, which are summarized in the following table.

KeywordGeneratorMultiple Stream and Substream SupportApproximate Period In Full Precision
mt19937arMersenne twister (used by default stream at MATLAB startup)No219937-1
dsfmt19937SIMD-oriented fast Mersenne twister No219937-1
mcg16807Multiplicative congruential generatorNo231-2
mlfg6331_64Multiplicative lagged Fibonacci generatorYes2124 (251 streams of length 272)
mrg32k3aCombined multiple recursive generatorYes2191 (263 streams of length 2127)
philox4x32_10Philox 4x32 generator with 10 roundsYes2193 (264 streams of length 2129)
threefry4x64_20Threefry 4x64 generator with 20 roundsYes2514 (2256 streams of length 2258)
shr3congShift-register generator summed with linear congruential generatorNo264
swb2712Modified subtract with borrow generatorNo21492

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.