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,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.
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.
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.
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.
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.