Accelerating the pace of engineering and science

# Documentation Center

• Trials

## Create Arrays of Random Numbers

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.