Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# randi

Uniformly distributed pseudorandom integers

## Syntax

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

## Description

r = randi(imax) returns a pseudorandom scalar between 1 and imax. This is the same as r = randi(imax,1).

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

r = randi(imax,sz1,...,szN) returns a sz1-by-...-by-szN array of pseudorandom integers 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.

r = randi(imax,sz) returns an array of pseudorandom integers where the size vector, sz, defines size(r). For example, randi(10,[3,4]) returns a 3-by-4 array of pseudorandom integers between 1 and 10.

 Note   The size inputs sz1,...,szN, as well as the elements of the size vector sz, should be nonnegative integers. Negative integers are treated as 0.

r = randi(imax,classname) returns a pseudorandom integer where the string, classname specifies the data type. classname can be 'single', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32', or 'uint32'.

r = randi(imax,n,classname) returns an n-by-n array of pseudorandom integers of data type classname.

r = randi(imax,sz1,...szN,classname) returns a sz1-by-...-by-szN array of pseudorandom integers of data type classname.

r = randi(imax,sz,classname) returns an array of pseudorandom integers where the size vector, sz, defines size(r) and classname defines class(r).

r = randi(imax,'like',p) returns a pseudorandom integer of the same data type as the numeric variable, p.

r = randi(imax,n,'like',p) returns an n-by-n array of pseudorandom integers like p.

r = randi(imax,sz1,...szN,'like',p) returns a sz1-by-...-by-szN array of pseudorandom integers like p.

r = randi(imax,sz,'like',p) returns an array of pseudorandom integers like p where the size vector, sz, defines size(r).

r = 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.

If you specify a distribution range using two numbers, [imin,imax], both numbers must be integers that satisfy iminimax. If you specify the range using only the upper bound, imax, it must be a positive integer (greater than zero).

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

## Examples

### Example 1

Generate a 100-by-1 array of integer values from the uniform distribution on the set 1:10:

`r = randi(10,100,1);`

### Example 2

Generate a 100-by-1 array of integers drawn uniformly from 1:10:

`r = randi(10,100,1,'uint32');`

### Example 3

Generate a 100-by-1 array of integer values drawn uniformly from -10:10:

`r = randi([-10 10],100,1);`

### Example 4

Reset the random number generator used by rand, randi, and randn to its default startup settings, so that randi produces the same random numbers as if you restarted MATLAB®:

```rng('default');
randi(10,1,5)

ans =
9    10     2    10     7```

### Example 5

Save the settings for the random number generator used by rand, randi, and randn, generate 5 values from randi, restore the settings, and repeat those values:

```s = rng;
i1 = randi(10,1,5)
i1 =
1     3     6    10    10
rng(s);
i2 = randi(10,1,5)
i2 =
1     3     6    10    10```

i2 contains exactly the same values as i1.

### Example 6

Reinitialize the random number generator used by rand, randi, and randn with a seed based on the current time. randi returns different values each time you do this. Note that it is usually not necessary to do this more than once per MATLAB session:

```rng('shuffle');
randi(10,1,5);```