Matlab

randi (Random integer) in MatLab

This article explains how to create arrays of random integers using the MATLAB randi() function. We also go over the main features of this function and show you a complete description of the input arguments and flags, the data types each of them supports, and the processing modes in different dimensions.This article includes pictures and practical examples that you can easily run in the MATLAB command console to understand this function better.

MATLAB randi() syntax

c = randi ( imax)

c = randi( imax, n)

c = randi( imax, h)

c = randi( imax, h1…. hn)

c = randi( imax, typename)

c = randi( imax, n, typename)

c = randi( imax, h, typename)

c = randi( imax, h1…. hn, typename)

c = randi( imax, like, p)

c = randi( imax, n, like, p)

c = randi( imax, h, like, p)

c = randi( imax, h1.… hn, like, p)

c = randi([min, imax],___)

c = randi(s,___)

MATLAB randi() description and examples

The MATLAB function randi() is used to create two-dimensional or multidimensional arrays of random integer values. This function returns in “c”, a scalar, vector, or matrix with random integer values evenly distributed among all elements. The range of random values and the output data type and working dimensions can be specified by the input arguments in the function call.

The input arguments and data types accepted by this function are explained below.

imax: Select the maximum value of random numbers generated by randi(). The data types accepted by this input are: positive integer

imin: Select the minimum value from which it will generate the random numbers. For example, if “imin” =50, randi() will generate random numbers greater than 50.

n: Specifies the size of the square array of random values generated by randi(). This input’s data types are single, double, int8, int16, int32, int64, uint8, uint16, uint32, or uint64.

h: Specifies the size of each dimension of the array to create. The type of data accepted by this input are: positive integer

typename: specifies the data type of the array to be generated. If typename = uint32, the output array will contain elements of type uint32. The data types that this input accepts are: ‘single’, ‘double’, ‘int8’, ‘int16’, ‘int32’, ‘int64’, ‘uint8’, ‘uint16’, ‘uint32’ or ‘uint64’

like: Specifies the data type of the elements in the output array. If randi() is called with the “like” flag, the output array will contain elements with the same data type as “p”.

p: If randi() is called with the “like” flag, the output array will contain elements with the same data type as “p”.

How to generate a scalar containing an integer random number with MATLAB’s randi() function

In the following example, we will see how to create a scalar “s” with a random number using the randi() function. We call the function and specify in “imax” the maximum integer value between which the random value must lie. In this case, it is 10.

c = randi (10)

In this case, randi() will return a scalar with a random integer value from 1 to 10.

c = 9

How to create a square matrix of random numbers with MATLAB randi() function

Let us see how we can use the randi() function to create a square array of random integers between 1 and 100. To do this, we will use the following syntax:

c= randi(imax, n)

With the input “imax”, we specify the maximum value of the random integer so that the randi() function will only produce values smaller than “imax” or, in this case, 100. With the input “n”, we specify the number of rows/columns of the square matrix we will create, in this case, 3×3.

c = randi (100, 3)

As a result, randi() created a 3 x 3 array with random numbers from 1 to 100.

c =

91 64 55

13 10 96

92 28 97

How to select the range of random numbers generated by the MATLAB randi() function

Now we will see how to select the minimum and maximum range between which the random numbers lie. In the following example, we will show you how to create an array of random numbers between 50 and 100. To do this, we call the randi() function by passing in its input arguments a vector with the inputs “imin” and “imax” with the ranges minimum and maximum as follows:

c = randi([imin, imax],n)
c = randi ([50, 100],3)
c =
58 74 71

99 90 96

98 57 90

As shown in the figure, the result will be a 3×3 matrix with random numbers from 50 to 100.

How to select the output data type in the MATLAB randi() function

The data type of the output array elements can be specified by typing “typename”. Now we will see how to create a square array with elements of type uint16 using the randi() function.

c = randi (99, 3, ‘uint16’)

In this case, the data type of the elements in the created array is uint16.

c = 3×3 uint16 matrix

95 85 76

65 93 74

4 68 39

How to create a 3D array of random numbers with MATLAB’s randi() function

To create a multidimensional array with randi(), we call the function by specifying in the input “h” the size of the array and the vector of dimensions as shown below

c = randi (99, [3, 4, 3])

In this case, the randi() function returns a 3D array of random numbers with the sizes and dimensions specified in the size vector.

Conclusion

In this article, we explained how to create 2D and 3D arrays of random numbers using the MATLAB randi() function. We also show you the different input arguments, flags, and data supported by these functions. We have applied this function’s different variations and show the most commonly used calling methods with practical examples and pictures. We hope this article has been useful to you. See other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).