Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

randi - Uniformly distributed pseudorandom integers

Syntax

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

Description

randi(imax) returns a random integer on the interval1:imax. r = randi(imax,n) returns an n-by-n matrix containing pseudorandom integer values drawn from the discrete uniform distribution on 1:imax. randi(imax,m,n) or randi(imax,[m,n]) returns an m-by-n matrix. randi(imax,m,n,p,...) or randi(imax,[m,n,p,...]) returns an m-by-n-by-p-by-... array. 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 internal state of the uniform pseudorandom number generator that underlies rand, randi, and randn. randi uses one uniform value from that default stream to generate each integer value. Control the default stream using its properties and methods. See @RandStream for details about the default stream.

Resetting the default stream to the same fixed state allows computations to be repeated. Setting the stream to different states leads to unique computations, however, it does not improve any statistical properties. Since the random number generator is initialized to the same state every time MATLAB software starts up, rand, randn, and randi will generate the same sequence of numbers in each session until the state is changed.

Examples

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

r = randi(10,100,1);

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

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

Generate integer values drawn uniformly from -10:10.

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

Replace the default stream at MATLAB startup, using a stream whose seed is based on clock, so that randi will return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session.

RandStream.setDefaultStream ... 
     (RandStream('mt19937ar','seed',sum(100*clock)));
randi(100,1,5)

Save the current state of the default stream, generate 5 integer values, restore the state, and repeat the sequence.

defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
i1 = randi(10,1,5)
defaultStream.State = savedState;
i2 = randi(10,1,5) %contains exactly the same values as i1

See Also

rand, randn, @RandStream, randi (RandStream), getDefaultStream (RandStream)

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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