# randi

Uniformly distributed pseudorandom integers

## Syntax

• ``X = randi(imax)``
• ``X = randi(imax,n)``
example
• ``X = randi(imax,sz1,...,szN)``
example
• ``X = randi(imax,sz)``
example
• ``X = randi(imax,classname)``
• ``X = randi(imax,n,classname)``
• ``X = randi(imax,sz1,...,szN,classname)``
example
• ``X = randi(imax,sz,classname)``
• ``X = randi(imax,'like',p)``
• ``X = randi(imax,n,'like',p)``
• ``X = randi(imax,sz1,...,szN,'like',p)``
• ``X = randi(imax,sz,'like',p)``
example
• ``X = randi([imin,imax],___)``

## Description

````X = randi(imax)` returns a pseudorandom scalar integer between `1` and `imax`.```

example

````X = randi(imax,n)` returns an `n`-by-`n` matrix of pseudorandom integers drawn from the discrete uniform distribution on the interval [`1`,`imax`].```

example

````X = randi(imax,sz1,...,szN)` returns an `sz1`-by-...-by-`szN` array where `sz1,...,szN` indicates the size of each dimension. For example, `randi(10,3,4)` returns a 3-by-4 array of pseudorandom integers between 1 and 10.```

example

````X = randi(imax,sz)` returns an array where size vector `sz` defines `size(X)`. For example, `randi(10,[3,4])` returns a 3-by-4 array of pseudorandom integers between 1 and 10.```
````X = randi(imax,classname)` returns a pseudorandom integer where `classname` specifies the data type. `classname` can be `'single'`, `'double'`, `'int8'`, `'uint8'`, `'int16'`, `'uint16'`, `'int32'`, or `'uint32'`.```
````X = randi(imax,n,classname)` returns an `n`-by-`n` array of data type `classname`.```

example

````X = randi(imax,sz1,...,szN,classname)` returns an `sz1`-by-...-by-`szN` array of data type `classname`..```
````X = randi(imax,sz,classname)` returns an array where size vector `sz` defines `size(X)` and `classname` defines `class(X)`.```
````X = randi(imax,'like',p)` returns a pseudorandom integer like `p`; that is, with the same data type (class).```
````X = randi(imax,n,'like',p)` returns an `n`-by-`n` array like `p`.```
````X = randi(imax,sz1,...,szN,'like',p)` returns an `sz1`-by-...-by-`szN` array like `p`.```

example

````X = randi(imax,sz,'like',p)` returns an array like `p` where size vector `sz` defines `size(X)`.```
````X = randi([imin,imax],___)` returns an array containing integers drawn from the discrete uniform distribution on the interval [`imin`,`imax`], using any of the above syntaxes.```

The sequence of numbers produced by `randi` is determined by the settings of the uniform random number generator that underlies `rand`, `randn`, and `randi`. The `randi` function uses one uniform random value to create each integer random value. You can control that shared random number generator using `rng`.

## Examples

collapse all

### Square Matrix of Random Integers

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 = randi(10,5)`
```r = 4 3 2 10 5 6 5 4 10 6 5 1 2 1 10 7 10 5 8 5 7 2 4 3 10```

### Random Integers Within Specified Interval

Generate a 10-by-1 column vector of uniformly distributed random integers from the sample interval `[-5,5]`.

`r = randi([-5,5],10,1)`
```r = 3 4 -4 5 1 -4 -2 1 5 5```

### Control Random Number Generation

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)```
```r = 1 6 9 7 2```

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)```
```r1 = 1 6 9 7 2```

Always use the `rng` function (rather than the `rand` or `randn` functions) to specify the settings of the random number generator. For more information, see Replace Discouraged Syntaxes of rand and randn.

### 3-D Array of Random Integers

Create a 3-by-2-by-3 array of uniformly distributed random integers between 1 and 500.

`X = randi(500,[3,2,3])`
```X(:,:,1) = 185 79 231 428 491 323 X(:,:,2) = 189 242 96 61 215 295 X(:,:,3) = 114 126 193 146 292 309```

### Random Integers of Other Data Types

Create a 1-by-4 vector of random numbers whose elements are of type `int16`.

```r = randi(100,1,4,'int16') ```
```r = 28 55 96 97 ```
`class(r)`
```ans = int16```

### Size Defined by Existing Array

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)```
```X = 3 10 9 8```

It is a common pattern to combine the previous two lines of code into a single line:

`X = randi(10,size(A));`

### Size and Numeric Data Type Defined by Existing Array

Create a 2-by-2 matrix of 8-bit signed integers.

```p = int8([3 2; -2 1]); ```

Create an array of random integers that is the same size and data type as `p`.

```X = randi(10,size(p),'like',p) ```
```X = 4 2 6 10 ```
`class(X)`
```ans = int8```

## Input Arguments

collapse all

### `imax` — Largest integer in sample intervalpositive integer

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)`

### `imin` — Smallest integer in sample interval`1` (default) | scalar integer

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.

### `n` — Size of square matrixinteger value

Size of square matrix, specified as an integer value.

• If `n` is `0`, then `X` is an empty matrix.

• If `n` is negative, then it is treated as `0`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `sz1,...,szN` — Size of each dimension (as separate arguments)two or more integer values

Size of each dimension, specified as separate arguments of integer values.

• If the size of any dimension is `0`, then `X` 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`

### `sz` — Size of each dimension (as a row vector)integer values

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`, then `X` 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`

### `classname` — Data type (class) to create`'double'` (default) | `'single'` | `'int8'` | `'uint8'` | ...

Output class, specified as the string `'double'`, `'single'`, `'int8'`, `'uint8'`, `'int16'`, `'uint16'`, `'int32'`, `'uint32'`, or the name of another class that provides `randi` support.

Example: `randi(5,5,'int8')`

Data Types: `char`

### `p` — Prototype of array to createnumeric array

Prototype of array to create, specified as a numeric array.

Example: `randi(5,5,'like',p)`

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `uint8` | `uint16` | `uint32`
Complex Number Support: Yes

expand all

### Tips

• The arrays returned by `randi` might contain repeated integer values. This behavior is sometimes referred to as sampling with replacement. Use `randperm` if you require all unique values.