Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the 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 =

   -10    10

Was this topic helpful?