Accelerating the pace of engineering and science

• 평가판
• 제품 업데이트

# randi

Uniformly distributed pseudorandom integers

## Syntax

r = randi(imax,n)
r = randi(imax,m,n)
r = randi(imax,[m,n])
r = randi(imax,p1,...,pn)
r = randi(imax,[p1,...,pn])
r = randi(imax)
r = randi(imax,size(A))
r = randi([imin,imax],...)
r = randi(..., classname)

## Description

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

r = randi(imax,m,n) and r = randi(imax,[m,n]) return an m-by-n matrix of numbers. Both m and n must be nonnegative integer values. If either m or n is negative, then it is treated as 0.

r = randi(imax,p1,...,pn) and r = randi(imax,[p1,...,pn]) return an n-dimensional array.

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

r = randi(imax,size(A)) returns an array the same size as A.

r = randi([imin,imax],...) returns an array containing integer values drawn from the discrete uniform distribution on the interval [imin,imax].

r = randi(..., classname) returns an array of integer values of class classname. classname does not support 64-bit integers.

Specify the size arguments, m, n or p1,..., pn, as nonnegative integers. If you pass a negative size value, it will be treated as 0.

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