randi
Uniformly distributed random integers
Syntax
Description
X = randi(
returns a random
scalar integer between imax
)1
and imax
.
X = randi(
returns an 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.
X = randi(___,
returns an array of random integers between typename
)1
and
imax
of data type typename
. The
typename
input can be "single"
,
"double"
, "int8"
,
"uint8"
, "int16"
,
"uint16"
, "int32"
,
"uint32"
, or "logical"
. You can use
any of the input arguments in the previous syntaxes.
X = randi(
generates integers from random number stream 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
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