# randi

Array of random integers

## Syntax

``R = randi(imax,n)``
``R = randi(imax,size1,...,sizeN)``
``R = randi(imax,size)``
``R = randi(imax,n,arraytype)``
``R = randi(imax,size1,...,sizeN,arraytype)``
``R = randi(imax,size,arraytype)``
``R = randi(imax,n,datatype,arraytype)``
``R = randi(imax,size1,...,sizeN,datatype,arraytype)``
``R = randi(imax,size,datatype,arraytype)``
``R = randi(imax,n,'like',P)``
``R = randi(imax,size1,...,sizeN,'like',P)``
``R = randi(imax,size,'like',P)``
``R = randi(imax,n,datatype,'like',P)``
``R = randi(imax,size1,...,sizeN,datatype,'like',P)``
``R = randi(imax,size,datatype,'like',P)``
``C = randi(imax,n,codist)``
``C = randi(imax,size1,...,sizeN,codist)``
``C = randi(imax,size,codist)``
``C = randi(imax,n,datatype,codist)``
``C = randi(imax,size1,...,sizeN,datatype,codist)``
``C = randi(imax,size,datatype,codist)``
``C = randi(___,codist,'noCommunication')``
``C = randi(___,codist,'like',P)``
``C = randi([imin imax], ___)``

## Description

````R = randi(imax,n)` returns an n-by-n distributed matrix with random integer values in the range `[1,imax]`.```
````R = randi(imax,size1,...,sizeN)` returns a size1-by-...-by-sizeN distributed matrix with random integer values in the range `[1,imax]`. size1-by-...-by-sizeN are separated arguments that indicates the size of each dimension of the generated matrix.```
````R = randi(imax,size)` returns an array whose size is defined by the size vector size.```

example

````R = randi(imax,n,arraytype)` returns an n-by-n matrix with underlying class of double, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```
````R = randi(imax,size1,...,sizeN,arraytype)` returns a size1-by-...-by-sizeN matrix with underlying class of double, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```
````R = randi(imax,size,arraytype)` returns an array whose size is defined by the size vector size with underlying class of double, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```

example

````R = randi(imax,n,datatype,arraytype)` returns an n-by-n matrix with underlying class of `datatype`, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```
````R = randi(imax,size1,...,sizeN,datatype,arraytype)` returns a size1-by-...-by-sizeN matrix with underlying class of `datatype`, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```
````R = randi(imax,size,datatype,arraytype)` returns an array whose size is defined by the size vector size with underlying class of `datatype`, random integer values in the range `[1,imax]`, and the type specified by `arraytype`.```
````R = randi(imax,n,'like',P)` returns an n-by-n array with random integer values in the range `[1,imax]` and the same type and underlying class (data type) as the prototype array, `P`.```
````R = randi(imax,size1,...,sizeN,'like',P)` returns a size1-by-...-by-sizeN array with random integer values in the range `[1,imax]` and the same type and underlying class (data type) as the prototype array, `P`.```
````R = randi(imax,size,'like',P)` returns an array whose size is defined by the size vector size with random integer values in the range `[1,imax]` and the same type and underlying class (data type) as the prototype array, `P`.```
````R = randi(imax,n,datatype,'like',P)` returns an n-by-n array with random integer values in the range `[1,imax]`, the specified underlying class (`datatype`), and the same type as the prototype array, `P`.```
````R = randi(imax,size1,...,sizeN,datatype,'like',P)` returns a size1-by-...-by-sizeN array with random integer values in the range `[1,imax]`, the specified underlying class (`datatype`), and the same type as the prototype array, `P`.```
````R = randi(imax,size,datatype,'like',P)` returns an array whose size is defined by the size vector size with random integer values in the range `[1,imax]`, the specified underlying class (`datatype`), and the same type as the prototype array, `P`.```

example

````C = randi(imax,n,codist)` returns an n-by-n codistributed array with random integer values in the range `[1,imax]` and underlying class of double. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = randi(imax,size1,...,sizeN,codist)` returns a size1-by-...-by-sizeN codistributed array with random integer values in the range `[1,imax]` and underlying class of double.```
````C = randi(imax,size,codist)` returns a codistributed array whose size is defined by the size vector size with random integer values in the range `[1,imax]` and underlying class of double.```
````C = randi(imax,n,datatype,codist)` returns an n-by-n codistributed array with random integer values in the range `[1,imax]` and underlying class of `datatype`. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```

example

````C = randi(imax,size1,...,sizeN,datatype,codist)` returns a size1-by-...-by-sizeN codistributed array with random integer values in the range `[1,imax]` and underlying class of `datatype`.```
````C = randi(imax,size,datatype,codist)` returns a codistributed array whose size is defined by the size vector size with random integer values in the range `[1,imax]` and underlying class of `datatype`.```
````C = randi(___,codist,'noCommunication')` specifies that no interworker communication is to be performed when constructing a codistributed array, skipping some error checking steps.```
````C = randi(___,codist,'like',P)` creates a codistributed array of random integer values with the specified range, size, underlying class, and distribution scheme. If either the class or codistributor argument is omitted, the characteristic is acquired from the codistributed array, `P`.```

example

````C = randi([imin imax], ___)` returns an array of random integers values in the range `[imin,imax]`, using any of the above syntaxes.```

## Examples

collapse all

Create a 1000-by-1000 distributed array of `randi` values from 1 to 100, with underlying class double.

```D = randi(100,1000,'distributed'); ```

Create a 1000-by-1000 codistributed double matrix of `randi` values from 0 to 12, distributed by its second dimension (columns).

```spmd(4) C = randi([0 12],1000,'codistributed'); end```

With four workers, each worker contains a 1000-by-250 local piece of `C`.

Create a 1000-by-1000 codistributed `single` matrix of `randi` values from 1 to 4, distributed by its columns.

```spmd(4) codist = codistributor('1d',2,100*[1:numlabs]); C = randi(4,1000,1000,'single',codist); end```

Each worker contains a 100-by-`labindex` local piece of `C`.

Create a 1000-by-1000 gpuArray of `randi` values from -50 to 50, with underlying class `double`:

```G = randi([-50 50],1000,'double','gpuArray'); ```

## Input Arguments

collapse all

Maximum integer in the range, specified as an integer value.

Minimum integer in the range, specified as an integer value.

Size of the generated array, specified as an integer value.

Size of each dimension of the generated array, specified as separate arguments of two or more 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.

Type of the generated array, specified as either `'distributed'`, `'codistributed'`, or `'gpuArray'`.

Underlying class of the array, that is the data type of its elements, specified as one of these options:

• `'double'`

• `'single'`

• `'int8'`

• `'uint8'`

• `'int16'`

• `'uint16'`

• `'int32'`

• `'uint32'`

• `'int64'`

• `'uint64'`

Distribution scheme for creating the codistributed array, specified as a codistributor object. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`. To use the default distribution scheme, you can specify a codistributor constructor without arguments.

Prototype of array to create, specified as an array.

## Output Arguments

collapse all

Array of random integers, returned as either a distributed array, a codistributed array, or a gpuArray.

Array of random integers, returned as a codistributed array.