randi
Uniformly distributed random integers
Syntax
Description
returns an X
= randi(imax
,sz1,...,szN
)sz1
-by-...-by-szN
array where
sz1,...,szN
indicate the size of each dimension. For
example, randi(10,3,4)
returns a 3-by-4 array of random
integers between 1 and 10.
generates integers from random number stream X
= randi(s
,___)s
instead of the
default global stream. To create a stream, use RandStream
. You can specify s
followed by any of the input argument combinations in previous syntaxes.
Examples
Input Arguments
Output Arguments
More About
Tips
The sequence of numbers produced by
randi
is determined by the internal settings of the uniform pseudorandom number generator that underliesrand
,randi
, andrandn
. You can control that shared random number generator usingrng
.The arrays returned by
randi
can contain repeated integer values. This behavior is sometimes referred to as sampling with replacement. Userandperm
if you require all unique values.If
imin
andimax
are outside the range of the output type (as specified bytypename
or by the prototypep
), thenrandi
first creates random integers within the interval[imin,imax]
and converts any resulting out-of-range integers to the minimum or maximum value of the output type. For example:rng default; r = randi([-10 10],1,10)
r = 7 9 -8 9 3 -8 -5 1 10 10
rng default; r = randi([-10 10],1,10,"logical")
r = 1×10 logical array 1 1 0 1 1 0 0 1 1 1
Extended Capabilities
Version History
Introduced in R2008bSee Also
rand
| randn
| rng
| RandStream
| randperm