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.
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 function returns floating-point numbers between 0 and
1 that are drawn from a uniform distribution. For example, create a 1000-by-1 column
vector containing real floating-point numbers drawn from a uniform
rng("default") r1 = rand(1000,1);
r1are in the open interval (0,1). A histogram of these values is roughly flat, which indicates a fairly uniform sampling of numbers.
randi function returns
values drawn from a discrete uniform distribution. For example, create a 1000-by-1
column vector containing integer values drawn from a discrete uniform
r2 = randi(10,1000,1);
r2are in the close interval [1, 10]. A histogram of these values is roughly flat, which indicates a fairly uniform sampling of integers between 1 and 10.
randn function returns arrays of real floating-point
numbers that are drawn from a standard normal distribution. For example, create a
1000-by-1 column vector containing numbers drawn from a standard normal
r3 = randn(1000,1);
r3looks like a roughly normal distribution whose mean is 0 and standard deviation is 1.
You can use the
randperm function to create a
double array of random integer values that have no repeated
values. For example, create a 1-by-5 array containing integers randomly selected
from the range [1,
r4 = randperm(15,5);
randi, which can return an array containing repeated values, the array returned by
randpermhas 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 table.
|Value||Generator Name||Generator Keyword|
|SIMD-Oriented Fast Mersenne Twister||dsfmt19937|
|Combined Multiple Recursive||mrg32k3a|
|Multiplicative Lagged Fibonacci||mlfg6331_64|
|Philox 4x32 generator with 10 rounds||philox4x32_10|
|Threefry 4x64 generator with 20 rounds||threefry4x64_20|
|Legacy MATLAB version 4.0 generator||mcg16807|
|Legacy MATLAB version 5.0 uniform generator||swb2712|
|Legacy MATLAB version 5.0 normal generator||shr3cong|
rng function to set the seed and
generator used by the
rng(0,"twister") sets the seed to 0 and the
generator algorithm to Mersenne Twister. To avoid repetition of random number arrays
when MATLAB restarts, see Why Do Random Numbers Repeat After Startup?
For more information about controlling the random number generator's state to repeat calculations using the same random numbers, or to guarantee that different random numbers are used in repeated calculations, see Controlling Random Number Generation.
Starting in R2023b, you can set the default algorithm and seed in MATLAB preferences. If you do not change these preferences, then
rng uses the factory value of
for the Mersenne Twister generator with seed 0, as in previous releases. For more
information, see Default Settings for Random Number Generator and Reproducibility for Random Number Generator.
Random Number Data Types
rng("default") A = rand(1,5); class(A)
ans = 'double'
To specify the class as double explicitly:
rng("default") B = rand(1,5,"double"); class(B)
ans = 'double'
ans = 1
rng("default") A = rand(1,5,"single"); class(A)
ans = 'single'
The values are the same as if you had cast the double precision values from the previous example. The random stream that the functions draw from advances the same way regardless of what class of values is returned.
A = 0.8147 0.9058 0.1270 0.9134 0.6324 B = 0.8147 0.9058 0.1270 0.9134 0.6324
randi supports both integer types and
single or double precision.
A = randi([1 10],1,5,"double"); class(A)
ans = 'double'
B = randi([1 10],1,5,"uint8"); class(B)
ans = 'uint8'