Create Arrays of Random Numbers
R2026bMATLAB® uses deterministic algorithms to generate pseudorandom numbers. These numbers are not truly random and independent in the mathematical sense, but they pass statistical tests of randomness and independence, and you can reproduce the sequence of random numbers 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 lets you control the seed and
algorithm that generates random numbers.
Generate Random Numbers
You can use the rand, randi, randn,
and randperm functions to generate random numbers that follow
different types of distributions. The rand function generates
random floating-point numbers between 0 and 1 that are drawn from a uniform
distribution. For example, create a 10,000-by-1 column vector containing real
floating-point numbers drawn from a uniform
distribution.
rng("default")
n = 10000;
r1 = rand(n,1);r1 are in the open
interval (0,1). Plot a histogram of these values. The histogram is roughly flat,
indicating that the sample of values follows a uniform distribution.histogram(r1) xlabel("Value") ylabel("Counts") title("Histogram of rand(" + n + ",1)")
The randi function generates random double integer
values drawn from a discrete uniform distribution. For example, create a 10,000-by-1
column vector containing integer values in the closed interval [1,10] drawn from a
discrete uniform distribution.
n = 10000; r2 = randi(10,n,1);
histogram(r2) xlabel("Value") ylabel("Counts") title("Histogram of randi(10," + n + ",1)")
The randn function generates random real floating-point numbers that are
drawn from a standard normal distribution. For example, create a 20,000-by-1 column
vector containing numbers drawn from a standard normal
distribution.
n = 20000; r3 = randn(n,1);
histogram(r3) xlabel("Value") ylabel("Counts") title("Histogram of randn(" + n + ",1)")
The randperm function generates a double array of random
integers without repeated values. For example, create a 1-by-10 array containing
integers randomly selected from the interval
[1,15].
r4 = randperm(15,10);
randi, which can return an array containing repeated values,
randperm returns an array with no repeated values. Plot a
histogram of these values. Each integer appears at most once in the histogram.histogram(r4) xticks(1:15) xlim([0 16]) yticks([0 1]) xlabel("Value") ylabel("Counts") title("Histogram of randperm(15,10)")
Successive calls to any of these functions return different results. This behavior is useful for creating different arrays of random values.
Specify Random Number Seed and Generator
You can use the rng function to set the seed and generator
algorithm that the rand, randi,
randn, and randperm functions use. The seed
sets the initial state of a random number generator and enables you to reproduce a
sequence of random numbers. The generator algorithm determines the algorithm used to
produce a sequence of random numbers.
MATLAB provides the generator algorithms in this table.
| Name | Description |
|---|---|
"twister" (default) | Mersenne Twister |
"combRecursive" | Combined multiple recursive generator |
"multFibonacci" | Multiplicative lagged Fibonacci generator |
| 64-bit permuted congruential generator with double xor-shift multiply |
"philox" | Philox 4x32 generator with 10 rounds |
"simdTwister" | SIMD-oriented fast Mersenne Twister |
"threefry" | Threefry 4x64 generator with 20 rounds |
| Xor-shift-rotate generator with 256-bit state and double addition |
Use the rng function to set the seed and generator used by the rand, randi, randn, and randperm functions.
For example, rng(0,"twister") sets the seed to 0 and the generator
algorithm to the 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 state of the random number generator so that you can repeat calculations using the same random numbers, or to guarantee that different random numbers are used in repeated calculations, see Control Random Number Generation.
You can change the default seed and algorithm for the random
number generator from the MATLAB Settings window (since R2023b). If you do not change these settings, then
rng uses the factory value of "twister" for
the Mersenne Twister generator with seed 0. For more information, see Default Settings for Random Number Generator and Reproducibility for Random Number Generator.
Specify Random Number Data Types
The rand and randn functions generate values in
double precision by default.
rng("default")
A = rand(1,5)A = 1×5
0.8147 0.9058 0.1270 0.9134 0.6324You can also explicitly specify the data type of the random numbers. For example, specify the
data type as double.
rng("default") B = rand(1,5,"double")
B = 1×5
0.8147 0.9058 0.1270 0.9134 0.6324tf = isequal(A,B)
ans = logical 1
rand and randn can also generate values in single
precision.
rng("default") A_single = rand(1,5,"single")
A_single = 1×5 single row vector
0.8147 0.9058 0.1270 0.9134 0.6324The values are the same as if you had cast the double-precision values from the previous
example to single precision. The random number stream that the rand
function draws from advances in the same way regardless of the output data type.
B_single = cast(B,"single");
isequal(A_single,B_single)ans = logical 1
The randi function supports single- and double-precision types and all
integer types except int64 and uint64.
A = randi([1 10],1,5,"double")A = 1×5
1 3 6 10 10B_uint8 = randi([1 10],1,5,"uint8")B_uint8 = 1×5 uint8 row vector
2 10 10 5 9See Also
rng | rand | randi | randn | randperm