| Contents | Index |
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)
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.
Note The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0. |
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.
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);
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 7Save 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 10i2 contains exactly the same values as i1.
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);@RandStream | rand | randi (RandStream) | randn | rng
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |