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.
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
rng('default') r1 = rand(1000,1);
r1is a 1000-by-1 column vector containing real floating-point numbers drawn from a uniform distribution. All the values in
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
r2 = randi(10,1000,1);
r2is a 1000-by-1 column vector containing integer values drawn from a discrete uniform distribution whose range is 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
r3 = randn(1000,1);
r3is a 1000-by-1 column vector containing numbers drawn from a standard normal distribution. A histogram of
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
r4 = randperm(15,5);
r4is a 1-by-5 array containing integers randomly selected from the range [1, 15]. Unlike
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.
MATLAB offers several generator algorithm options, which are summarized in the table.
|Value||Generator Name||Generator Keyword|
|Mersenne Twister (used by default stream at MATLAB startup)||mt19937ar|
|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
randperm functions. For
rng(0,'twister') reset the generator to its default
state. 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.
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'