The function randi creates matrices of random integers.

Description

randi generates random integers drawn from the discrete uniform distribution. Find in this book a complete explanation of this distribution, and others.

Syntax

X = randi([imin,imax],n) creates an n-by-n matrix X of random integers within the range $$[imin,imax]$$.

Another use is X = randi([imin,imax],sz1,…,szN), in which the size of the result is determined by the arguments sz1,…,szN.

The output of randi may contain repeated values.
% Random integer in the range [-5,5]
resultExample1 = randi([-5,5],1);
% Vector of random integers in the range [-10,0]
resultExample2 = randi([-10,0],1,5);
% Square matrix of random integers in the range [0,10]
resultExample3 = randi([0,10],3);
% Matrix of random integers in the range [0,100], with two rows and four columns
resultExample4 = randi([0,100],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 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 page in the MATLAB help documentation, and the other tutorials in this series.

doc randi;

Source code

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