Matlab

# How to Generate Uniformly Distributed Pseudorandom Integers in MATLAB Using randi()

Pseudorandom integers are numbers that are not random in actual. But they behave like random numbers. These numbers are generated using deterministic algorithms to determine the behavior of random numbers. These numbers have many applications in network security, cryptography, data science, machine learning, Monte Carlo simulations, and many other fields.

We can manually generate pseudorandom integers using mathematical algorithms, but this is not a practical approach in the current era where we have high-performing computing tools like MATLAB.

In this blog, we are going to explore how to generate uniformly distributed pseudorandom integers in MATLAB.

## How to Generate Uniformly Distributed Pseudorandom Integers in MATLAB Using the randi() Function?

A uniformly distributed pseudorandom integer is an integer that is evenly distributed over a specified range, meaning that each integer in the range has an equal probability of being generated. We can easily generate the uniformly distributed pseudorandom integers in MATLAB using the built-in randi() function. This function accepts two mandatory and some optional arguments as inputs and returns an array of uniformly distributed pseudorandom integers in MATLAB.

Syntax
There are different syntaxes for randi() functions in MATLAB, which are given below:

X = randi(r,n)
X = randi(r,sz1,...,szN)
X = randi(r,sz)
X = randi(___,datatype)

Here,
The function X = randi(r,n) yields an n-by-n square matrix of the uniformly distributed pseudorandom integers in the specified range r.

• If r is a scalar value, the pseudo-random integers will be created in the interval [1, r].
• If r is a vector, the pseudo-random integers will be generated in the range r(1) to r(2).

The function X = randi(r,sz1,…,szN) yields an array of uniformly distributed pseudorandom integers having dimensions sz1,sz2,…,szN ranges from 1 to imax.

The function X = randi(r,sz) yields to creation of an array of uniformly distributed pseudorandom integers having size sz where sz defines the size of the array X.

The function X = randi(___,’datatype’) yields the creation of an array of uniformly distributed pseudorandom integers having the specified data type i.e. int8, int,16, int32, or int64.

Examples
The given examples demonstrate how to create the pseudorandom integers in MATLAB using the randi() function.

Example 1: How to Create Pseudorandom Integers in MATLAB Using X = randi(r,n) Function?
This example generates the pseudorandom integers using the randi(r,n) function in MATLAB.

X = randi(100, 4)

Example 2: How to Generate Uniformly Distributed Pseudorandom Integers in MATLAB Using X = randi(r, sz1,sz2,…,szN) Function?
In this example, we generate an array of uniformly distributed pseudorandom integers in MATLAB using the X=randi(r,sz1,sz2,…,szN) function in MATLAB.

X = randi(100,2,5,3)

Example 3: How to Generate Pseudorandom Integers in MATLAB Using X = randi(r,sz) Function?
This MATLAB code generates the uniformly distributed pseudorandom integers using the randi(r,sz) function in MATLAB.

X = randi(100,[2 5 3])

Example 4: How to Generate Uniformly Distributed Pseudo Random Integers in MATLAB Using X = randi(r,n,datatype) Function?
In the given MATLAB code, we generate a matrix of uniformly distributed pseudorandom integers with the data type int16 using the randi() function in MATLAB.

X = randi(100,5,'int16')

## Conclusion

Pseudorandom integers play an important role in the fields of computing and data science. They can be defined as the numbers generated by deterministic algorithms. These numbers are not actually random numbers however, they determine the pattern and behavior of the random numbers. This guide elaborated on how to generate uniformly distributed pseudorandom integers in MATLAB using some examples.