randi - Uniformly distributed random integers - MATLAB (original) (raw)
Uniformly distributed random integers
Syntax
Description
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([imax](#buf2csg-imax))
returns a random scalar integer between 1
andimax
.
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([imax](#buf2csg-imax),[n](#buf2csg-n))
returns an n
-by-n
matrix of random integers drawn from the discrete uniform distribution on the interval [1
,imax
].
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([imax](#buf2csg-imax),[sz1,...,szN](#buf2csg-sz1szN))
returns an sz1
-by-...-by-szN
array wheresz1,...,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](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([imax](#buf2csg-imax),[sz](#buf2csg-sz))
returns an array where size vector sz
definessize(X)
. For example, randi(10,[3 4])
returns a 3-by-4 array of random integers between 1 and 10.
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi(___,[typename](#buf2csg-classname))
returns an array of random integers between 1
andimax
of data type typename
. Thetypename
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](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi(___,like=[p](#buf2csg-p))
returns an array of random integers like p
; that is, with the same data type and complexity (real or complex) as p
. You can specify either typename
or like
, but not both.
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([[imin](#buf2csg-imin),[imax](#buf2csg-imax)],___)
returns an array containing integers drawn from the discrete uniform distribution on the interval [imin
,imax
], using any of the above syntaxes.
[X](#mw%5F96306de0-b3bd-435e-9b1b-e119130506e7) = randi([s](#mw%5F3f3e135a-0842-44f9-989f-5004615a9bb7),___)
generates integers from random number stream 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
Generate a 5-by-5 matrix of random integers between 1 and 10. The first input to randi
indicates the largest integer in the sampling interval (the smallest integer in the interval is 1).
r = 5×5
9 1 2 2 7
10 3 10 5 1
2 6 10 10 9
10 10 5 8 10
7 10 9 10 7
Generate a 10-by-1 column vector of uniformly distributed random integers from the sample interval [-5,5]
.
r = 10×1
3
4
-4
5
1
-4
-2
1
5
5
Save the current state of the random number generator and create a 1-by-5 vector of random integers.
s = rng; r = randi(10,1,5)
Restore the state of the random number generator to s
, and then create a new 1-by-5 vector of random integers. The values are the same as before.
rng(s); r1 = randi(10,1,5)
Create a 3-by-2-by-3 array of uniformly distributed random integers between 1 and 500.
X = X(:,:,1) =
408 457 453 317 64 49
X(:,:,2) =
140 483 274 79 479 486
X(:,:,3) =
479 71 243 211 401 458
Create a 1-by-4 vector of random numbers between 1 and 100 whose elements are of type int16
.
r = randi(100,1,4,"int16")
r = 1×4 int16 row vector
82 91 13 92
Create a matrix of uniformly distributed random integers between 1 and 10 with the same size as an existing array.
A = [3 2; -2 1]; sz = size(A); X = randi(10,sz)
It is a common pattern to combine the previous two lines of code into a single line:
Create a 2-by-2 matrix of 8-bit signed integers.
Create an array of random integers that is the same size and data type as p
.
X = randi(10,size(p),like=p)
X = 2×2 int8 matrix
9 2
10 10
Since R2022a
Generate 10 random complex integers from the discrete uniform distribution over a square domain with real and imaginary parts in the interval [-5,5]
.
a = randi([-5,5],10,1,like=1i)
a = 10×1 complex
3.0000 + 4.0000i -4.0000 + 5.0000i 1.0000 - 4.0000i -2.0000 + 1.0000i 5.0000 + 5.0000i -4.0000 + 5.0000i 5.0000 + 0.0000i 3.0000 - 4.0000i -1.0000 + 5.0000i 3.0000 + 5.0000i
Since R2023a
Create a 5-by-5 matrix of random logical values (0s and 1s) with a discrete uniform distribution.
r = randi([0 1],5,"logical")
r = 5×5 logical array
1 0 0 0 1 1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1
Input Arguments
Largest integer in sample interval, specified as a positive integer.randi
draws values from the uniform distribution in the sample interval [1,imax]
.
Example: randi(10,5)
Smallest integer in sample interval, specified as a scalar integer.
Both imin
and imax
must be integers that satisfy imin
≤ imax
.
For example, randi([50,100],5)
returns a 5-by-5 matrix of random integers between (and including) 50 and 100.
Size of square matrix, specified as an integer value.
- If
n
is0
, thenX
is an empty matrix. - If
n
is negative, then it is treated as0
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Size of each dimension, specified as separate arguments of integer values.
- If the size of any dimension is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
randi
ignores trailing dimensions with a size of 1. For example,randi([5,10],3,1,1,1)
produces a 3-by-1 vector of random integers between 5 and 10.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:
- If the size of any dimension is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
randi
ignores trailing dimensions with a size of 1. For example,randi([5,10],[3 1 1 1])
produces a 3-by-1 vector of random integers between 5 and 10.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Data type (class) to create, specified as "double"
,"single"
, "int8"
,"uint8"
, "int16"
,"uint16"
, "int32"
,"uint32"
, "logical"
, or the name of another class that provides randi
support.
Example: randi(5,5,"int8")
Prototype of array to create, specified as a numeric or logical array.
Example: randi(5,5,like=p)
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| logical
Complex Number Support: Yes
Random number stream, specified as a RandStream object.
Example: s = RandStream("dsfmt19937"); randi(s,[5,10],[3 1])
Output Arguments
Output array, returned as a scalar, vector, matrix, or multidimensional array.
More About
The underlying number generator for randi
is a pseudorandom number generator, which creates a deterministic sequence of numbers that appear random. These numbers are predictable if the seed and the deterministic algorithm of the generator are known. While not truly random, the generated numbers pass various statistical tests of randomness, satisfying the independent and identically distributed (i.i.d.) condition, and justifying the name pseudorandom.
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 using rng. - 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
Usage notes and limitations:
- If used, the input argument
typename
must be"double"
,"single"
,"int8"
,"uint8"
,"int16"
,"uint16"
,"int32"
,"uint32"
, or"logical"
. Other classes are not supported, even if the class defines a staticrandi
method. - If used, size arguments
n
,sz
, andsz1,...,szN
must be fixed size. - The code generator uses double-precision math to compute single-precision outputs.
- In most cases, generated MEX files use the same random number state as MATLAB®. If you disable extrinsic calls or you call
randi
from inside aparfor
-loop, the generated MEX code and standalone code maintain their own random number state that is initialized to the MATLAB default, which isrng(0,"twister")
.
The randi
function supports GPU array input with these usage notes and limitations:
- You can specify
typename
as'gpuArray'
. If you specifytypename
as'gpuArray'
, the default underlying type of the array isdouble
.
To create a GPU array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = randi(imax,3,datatype,'gpuArray')
creates a 3-by-3 GPU array of random integers in the range[1 imax]
with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
'logical'
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
- You can also specify the numeric variable
p
as agpuArray
.
If you specifyp
as agpuArray
, the underlying type of the returned array is the same asp
. - To use the stream syntax,
randi(`s`,___)
, on a GPU,s
must be a parallel.gpu.RandStream (Parallel Computing Toolbox) object.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
- The stream syntax
randi(`s`,___)
is not supported forcodistributed
ordistributed
arrays. - You can specify
typename
as'codistributed'
or'distributed'
. If you specifytypename
as'codistributed'
or'distributed'
, the default underlying type of the returned array isdouble
.
To create a distributed or codistributed array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = randi(imax,3,datatype,'distributed')
creates a 3-by-3 distributed matrix of random integers in the range[1 imax]
with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
'logical'
- You can also specify
p
as acodistributed
ordistributed
array.
If you specifyp
as acodistributed
ordistributed
array, the underlying type of the returned array is the same asp
. - For additional
codistributed
syntaxes, see randi (codistributed) (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2008b
You can create a random logical array by specifying typename
as"logical"
or the prototype p
as a logical array. For example, see Random Logical Array.
The like
name-value argument supports both real and complex prototype arrays. For example:
r =
2.0000 + 2.0000i 2.0000 + 1.0000i 1.0000 + 2.0000i 1.0000 + 2.0000i
All syntaxes support this feature. Also, you can now use like
with a RandStream
object as the first input ofrandi
.
To generate random numbers with the same data type as an existing variable, use the syntax randi(__,'like',p)
. For example:
A = single(pi); r = randi(4,4,'like',A); class(r)
This feature is not available when passing aRandStream
object as the first input torandi
.
Specifying a dimension that is not an integer causes an error. Use floor to convert non-integer size inputs to integers.