MATLAB provides multiple functions to generate a scalar, a matrix, or a vector of random numbers. These functions generate different random numbers according to their functionalities. One function among these is randn() which allows us to generate normally distributed random numbers.
In this article, we are going to discover how to generate random numbers between 0 and 1 using the randn() function in MATLAB.
What is randn() Function in MATLAB?
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.
This function follows a simple syntax that is given below:
X = randn(n)
X = randn(sz1,...,szN)
Here:
X = randn generates a random scalar selected from the normal distribution.
X = randn(n) yields an n-by-n matrix having random numbers with a normal distribution.
X = randn(sz1,…,szN) returns a random number array of size sz1 by… by szN where sz1,…,szN denote the dimensions’ sizes. For example, randn(5,4) returns a 5-by-4 matrix.
How to Use randn() for Generating Random Numbers Between 0 and 1?
As we know, the randn() function generates any positive or negative real numbers by default. However, if we want to generate random numbers between 0 and 1 using this function, it can be possible by performing some mathematical operations like multiplying it with a small scalar value.
There is another function rand() in MATLAB for generating random numbers lying between 0 and 1 by default. However, if you are to stick with using the randn() function to generate random numbers between 0 and 1, here are a few easy examples that demonstrate how to generate random numbers between 0 and 1 using the randn() function.
Example 1
The given example generates a scalar random number lying between 0 and 1. For this purpose, we set a scale of 0.1 to multiply a number generated by the randn() function to convert it into a value that lies between 0 and 1. We also apply the abs() function to make it positive.
Example 2
In this example, we generate a 5-by-5 matrix of random numbers that lie between 0 and 1. For this purpose, we set a scale of 0.1 to multiply it with the matrix entries generated by the randn() function to convert them into values lying between 0 and 1. We also apply the abs() function to make all entries positive.
Example 3
In this MATLAB code, we generate a 5-by-4 matrix of random numbers that lie between 0 and 1. For this purpose, we set a scale of 0.1 to multiply it with the matrix entries generated by the randn() function to convert them into values lying between 0 and 1. We also apply the abs() function to make all entries positive.
Conclusion
The randn() is a built-in function in MATLAB that allows us to generate normally distributed random numbers with a mean of 0 and a standard deviation of 1. The random numbers generated by this function can be any real positive or negative values. However, we can generate random numbers between 0 and 1 using this function by performing some mathematical operations like scaling and taking the absolute value. This guide taught us how we can generate random numbers between 0 and 1 using the function with a few simple examples.