Skip to Main Content Skip to Search
Product Documentation

randi - Uniformly distributed pseudorandom integers

Syntax

r = randi(imax,n)
r = randi(imax,m,n)
r = randi(imax,[m,n])
r = randi(imax,m,n,p,...)
r = randi(imax,[m,n,p,...])
r = randi(imax)
r = randi(imax,size(A))
r = randi([imin,imax],...)
r = randi(..., classname)

Description

r = randi(imax,n) returns an n-by-n matrix containing pseudorandom integer values drawn from the discrete uniform distribution on 1:imax. r = randi(imax,m,n) or r = randi(imax,[m,n]) returns an m-by-n matrix. r = randi(imax,m,n,p,...) or r = randi(imax,[m,n,p,...]) returns an m-by-n-by-p-by-... array. r = randi(imax) returns a scalar. r = randi(imax,size(A)) returns an array the same size as A.

r = randi([imin,imax],...) returns an array containing integer values drawn from the discrete uniform distribution on imin:imax.

r = randi(..., classname) returns an array of integer values of class classname. classname does not support 64-bit integers.

The sequence of numbers produced by randi is determined by the settings of the uniform random number generator that underlies rand, randn, and randi. randi uses one uniform random value to create each integer random value. You can control that shared random number generator using rng.

Examples

Example 1

Generate integer values from the uniform distribution on the set 1:10:

r = randi(10,100,1);

Example 2

Generate an integer array of integers drawn uniformly from 1:10:

r = randi(10,100,1,'uint32');

Example 3

Generate integer values drawn uniformly from -10:10:

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

Example 4

Reset the random number generator used by rand, randi, and randn to its default startup settings, so that randi produces the same random numbers as if you restarted MATLAB:

rng('default');
randi(10,1,5)

ans =
     9    10     2    10     7

Example 5

Save the settings for the random number generator used by rand, randi, and randn, generate 5 values from randi, restore the settings, and repeat those values:

s = rng;
i1 = randi(10,1,5)
i1 =
     1     3     6    10    10
rng(s);
i2 = randi(10,1,5)
i2 =
     1     3     6    10    10

i2 contains exactly the same values as i1.

Example 6

Reinitialize the random number generator used by rand, randi, and randn with a seed based on the current time. randi returns different values each time you do this. Note that it is usually not necessary to do this more than once per MATLAB session:

rng('shuffle');
randi(10,1,5);

See Also

@RandStream | rand | randi (RandStream) | randn | rng

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS