This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Random Integers

This example shows how to create an array of random integer values that are drawn from a discrete uniform distribution on the set of numbers –10, –9,...,9, 10.

The simplest randi syntax returns double-precision integer values between 1 and a specified value, imax. To specify a different range, use the imin and imax arguments together.

First, initialize the random number generator to make the results in this example repeatable.

rng(0,'twister');

Create a 1-by-1000 array of random integer values drawn from a discrete uniform distribution on the set of numbers -10, -9,...,9, 10. Use the syntax, randi([imin imax],m,n).

r = randi([-10 10],1,1000);

Verify that the values in r are within the specified range.

r_range = [min(r) max(r)]
r_range = 1×2

   -10    10