Navigation

Operators and Keywords

Function List:

C++ API

: randi (imax)
: randi (imax, n)
: randi (imax, m, n, …)
: randi ([imin imax], …)
: randi (…, "class")

Return random integers in the range 1:imax.

Additional arguments determine the shape of the return matrix. When no arguments are specified a single random integer is returned. If one argument n is specified then a square matrix (n x n) is returned. Two or more arguments will return a multi-dimensional matrix (m x n x …).

The integer range may optionally be described by a two element matrix with a lower and upper bound in which case the returned integers will be on the interval [iminimax].

The optional argument class will return a matrix of the requested type. The default is "double".

The following example returns 150 integers in the range 1–10.

ri = randi (10, 150, 1)

Implementation Note: randi relies internally on rand which uses class "double" to represent numbers. This limits the maximum integer (imax) and range (imax - imin) to the value returned by the flintmax function. For IEEE floating point numbers this value is 2^{53} - 1.

See also: rand.

Package: octave