Generating Random Number in MATLAB


In MATLAB programming, we can use various types of built-in functions to generate different types of random numbers. Random numbers are nothing but numbers selected randomly from a set of numbers.

MATLAB provides the following four major functions to generate different types of random numbers depending on our requirements:

  • rand()

  • randn()

  • randi()

  • randperm()

Let us discuss each of these four functions in detail with the help of example program in MATLAB.

“rand” Function

The “rand()” function is a built-in function in MATLAB that can be used to generate random numbers distributed between 0 and 1 uniformly. In this function, we have to specify the dimensions of the random matrix as arguments.

Syntax

rand

We can use rand() function to generate a matrix of uniformly distributed random numbers, in that case the following syntax is to be followed:

rand(row, col)

For example, to generate a 3 x 5 matrix of random numbers,

rand(3, 5)

Now, let us consider some MATLAB programs to understand the operation of rand() function.

Example 

% MATLAB program to generate 6 uniformly distributed random numbers
for i = 1:6
   ran_num = rand;
   disp(ran_num)
end

Output

0.033219
0.5965
0.1006
0.4967
0.6671
0.1800

Example 

% MATLAB program to generate a 3 × 4 matrix of uniformly distributed random numbers
% Call the rand() function to generate a random matrix of size 3 × 4
Rand_Matrix = rand(3, 4);
% Display the randomly generated matrix
disp('The Random Matrix is:');
disp(Rand_Matrix);

Output

The Random Matrix is:
   0.9883   0.4337   0.8890   0.3114
   0.5068   0.7758   0.6320   0.1136
   0.5637   0.3904   0.3751   0.8236

Example

% MATLAB program to generate a random vector of length 6
% Call the rand() function to generate a random vector of length 6
Rand_Vector = rand(1, 6);
% Display the randomly generated vector
disp('The Random Vector is:');
disp(Rand_Vector);

Output

The Random Vector is:
   0.386889   0.313298   0.593926   0.268528   0.095952   0.965557

In the above MATLAB programs, we have used the “rand()” function to generate six random numbers, a matrix of size 3 x 4, and a random vector of length 6.

"randn" Function

In MATLAB, the randn() function is used to generate normally distributed random numbers. This function basically is used to generate random numbers, random matrix, or random vectors using a standard normal distribution with mean 0 and standard deviation 1.

Syntax

To generate a list of normally distributed random number:

randn

To generate a random matrix of size n x m:

randn(n, m)

To generate a random vector of length n:

randn(1, n)

Now, let us understand the implementation of the randn() function with the help of an example program.

Example

% MATLAB program to demonstrate the implementation of randn() function
% Generate 6 normally distributed random numbers
 disp('Random Numbers:')
for i = 1:6
   Rand_Num = randn;
   disp(Rand_Num);
end

% Generate a random matrix of size 4x3 with standard normal distribution
Rand_Matrix = randn(4, 3);
% Display the generated random matrix
disp('Random Matrix:');
disp(Rand_Matrix);

% Generate a random vector of length 6 with standard normal distribution
Rand_Vector = randn(1, 6);
% Display the generated random vector
disp('Random Vector:');

Output

Random Numbers:
0.039510
-0.1805
0.2246
-1.3301
0.5415
0.6542
Random Matrix:
  -0.6967  -0.5581   0.7305
  -0.2204  -0.2242   0.3702
  -1.6478  -0.9363   1.4140
   1.0097   0.5562  -0.2543
Random Vector:
  -0.4225   0.3729  -0.8782   0.4408   0.2229  -0.7132

Explanation

In this MATLAB program, the randn() function is used to generate 6 normally distributed random numbers, a random matrix of size 4x3 (Rand_Matrix), and a random vector of length 6 (Rand_Vector) from standard normal distribution.

“randi” Function

In MATLAB, the randi() function is used to generate pseudorandom integer numbers within a certain range. The randi() function basically generates random integer numbers between the specified minimum and maximum values using uniform distribution.

Syntax

To generate random integers between 'min' and 'max':

randi([min, max], 1)

To generate a random matrix of size n x m with integers between 'min' and 'max':

randi([min, max], n, m)

To generate a random vector of length n with integers between ‘min’ and ‘max’:

randi([min, max], 1, n)

Let us understand this implementation with the help of an example program.

Example 

% MATLAB program to demonstrate the implementation of randi() function
% Generate 6 random integers between 5 and 10
disp('Random Integers:')
for i = 1:6
   Rand_Int = randi([5, 10], 1);
   disp(Rand_Int);
end

% Generate a random matrix of size 3x4 with integers between 4 and 10
Rand_Matrix = randi([4, 10], 3, 4);
% Display the generated random matrix
disp('Random Matrix:');
disp(Rand_Matrix);

% Generate a random vector of length 6 with integers between 3 and 9
Rand_Vector = randi([3, 9], 1, 6);
% Display the generated random vector
disp('Random Vector:');
disp(Rand_Vector);

Output

Random Integers:
5
7
10
10
10
6
Random Matrix:
    4    4    8    6
    4   10    8    9
    5    5    9    4
Random Vector:
   9   4   7   9   8   4

Explanation

In this MATLAB program, the randi() function is used to generate 6 random integers between 5 and 10, a random matrix of size 3x4 (Rand_Matrix) and a random vector of length 6 (Rand_Vector).

“randperm” Function

In MATLAB, the “randperm()” function is used to generate a random permutation of integers. This function is helpful in shuffling or randomizing data.

Syntax

randperm(max_value)

Let us consider an example program to understand the implementation of the randperm() function.

Example

% MATLAB program to demonstrate the implementation of randperm()function
% Generate a random permutation of the integers from 1 to 7
Rand_Perm = randperm(7);
% Display the generated random permutation
disp('Random Permutation:');
disp(Rand_Perm);

Output

Random Permutation:
   1   2   6   7   4   3   5

Explanation

In this MATLAB program, the randperm() function is used to generate a random permutation of integers from 1 to 7. The result is stored in a variable “Rand_Perm”.

Conclusion

In this tutorial, we explained everything about random number generation in MATLAB. However, it is important to note that the values of the about outputs will be different each time you run the program, this is because the rand() function generates new random values every time.

Updated on: 18-Jul-2023

417 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements