Main Content

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:

rng('default')
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 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.

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 a double array of random integer values that have no repeated values. For example,

r4 = randperm(15,5);
r4 is 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 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 table.

ValueGenerator NameGenerator Keyword
'twister'Mersenne Twister (used by default stream at MATLAB startup)mt19937ar
'simdTwister'SIMD-oriented Fast Mersenne Twisterdsfmt19937
'combRecursive'Combined multiple recursivemrg32k3a
'multFibonacci'Multiplicative Lagged Fibonaccimlfg6331_64
'philox'Philox 4x32 generator with 10 roundsphilox4x32_10
'threefry'Threefry 4x64 generator with 20 roundsthreefry4x64_20
'v4'Legacy MATLAB version 4.0 generatormcg16807
'v5uniform'Legacy MATLAB version 5.0 uniform generatorswb2712
'v5normal'Legacy MATLAB version 5.0 normal generatorshr3cong

Use the rng function to set the seed and generator used by the rand, randi, randn, and randperm functions. For example, 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.

Random Number Data Types

rand and randn functions generate values in double precision by default.

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'
isequal(A,B)
ans = 
1

rand and randn can also generate values in single precision.

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,B
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'

See Also

| | | |

Related Topics