The functions rand and randn create matrices of random floating-point numbers.

## Description

rand generates random numbers in the open interval $$(0,1)$$ drawn from the standard uniform distribution. randn uses the standard normal distribution. Find in this book a complete explanation of both distributions, and others.

## Syntax

X = rand stores in X a random number. X = rand(n) creates an n-by-n matrix X of random values. X = rand(sz1,…,szN) creates a matrix X of random values, whose size is determined by the arguments of the function.

rand and randn share the same syntax.
% Random numbers drawn from the standard uniform distribution
resultExample1 = rand(3);
% Random numbers drawn from the standard normal distribution
resultExample2 = randn(3);
% Vector of random numbers
resultExample3 = rand(1,5);
% Matrix of random numbers, with two rows and four columns
resultExample4 = rand(2,4);

Note that your result in the preceding examples could be different due to the random nature of the operations.

I recommend the following books to learn more on random numbers in MATLAB.

1. This book was the winner of a 2017 Textbook Excellence Award. The first chapter covers random numbers, logical operators, and common elementary math functions, among other topics.

2. MATLAB for Engineers (5th Edition)

This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The third chapter covers random numbers, matrix sorting, and several data analysis functions, among other topics.

I also recommend the following pages in the MATLAB help documentation, and the other tutorials in this series.

doc rand;
doc randn;

## Source code

I hope you have learned how to generate random floating-point numbers in MATLAB. The source code developed in this tutorial is available at this page.