Matlab

How to Generate Random Numbers in MATLAB

MATLAB facilitates us with multiple functions to generate a scalar, a vector, or, a matrix of random numbers. These functions generate different random numbers in different distributions according to their functionalities. In this article, we will learn how to generate random numbers using different functions in MATLAB.

How to Create Random Numbers in MATLAB?

MATLAB includes multiple functions for generating random numbers that are listed below:

Now we are going to demonstrate the working of these functions to generate random numbers using examples.

1: Using rand() Function

the rand() is a built-in function in MATLAB that allows us to generate uniformly distributed random numbers lying between 0 and 1. This function can be used for generating a vector, a scalar, or, a matrix of random numbers. For example:

rand(5)

In the above MATLAB code, we generate a 5-by-5 matrix having random numbers lying between 0 and 1 using the rand(n) function. Here, we consider n = 5.

2: Using randi() Function

the randi() is a built-in function in MATLAB that is used for generating discrete uniformly distributed pseudorandom integers lying between 1 and specified value imax. This function can be used for generating a vector, a scalar, or, a matrix of random numbers. For example:

randi(10,5)

In the above example, we generate a 5-by-5 matrix of random numbers between 1 and imax using the randi(imax,n) function. Here, we consider imax = 10 and n = 5.

3: Using randn() Function

the randn() is a built-in function in MATLAB that is used for generating normally distributed random numbers with a mean of 0 and a standard deviation of 1. This function can be used for generating a vector, a scalar, or, a matrix of random numbers. The random numbers generated by this function can be any real positive or negative values. For example:

randn(5)

In the above example, we generate a 5-by-5 matrix of normally distributed random numbers using the randn(n) function. Here, we consider n = 5.

4: Using randperm() Function

The randperm() is a built-in function in MATLAB that allows us to generate a row vector having a random permutation of the integers from 1 to the specified number n. The function is only used for generating a row vector containing the random permutation of integers. For example:

randperm(5)

In this given MATLAB code, we generate a vector containing integer values randomly permuted from 1 to 5 using randperm(n) function for n = 5.

Conclusion

MATLAB facilitates us with built-in functions that are used for generating random numbers according to their functionalities. These functions are rand(), randi(), randn(), and, randperm() These functions can be used for generating a vector, a scalar, or, a matrix of random numbers. This tutorial taught us how to generate random numbers using all these functions.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.