Matlab

How to Use randn() Function in MATLAB with Examples

MATLAB is a valuable programming tool used by mathematicians and engineers to perform many tasks. One such task is generating normally distributed random numbers which can be easily performed using MATLAB’s built-in randn() function.

If you are not familiar with the working of the randn() function, this blog will help you in that case.

Why Do We Require Random Numbers in MATLAB?

Generating random numbers in MATLAB is useful in several scenarios, which are as follows:

  • You can use it to generate cryptographic keys that can be used for data security.
  • You can approximate the solution to a problem in a Monte Carlo simulation.
  • You can generate training data for algorithms involving machine learning using these random numbers.

What is the randn() Function in MATLAB?

The randn() is a built-in function in MATLAB used for generating normally distributed random numbers. This function can generate a single random value or a one-dimensional or multidimensional array of normally distributed random numbers depending on its arguments. The normally distributed random numbers are used in various scenarios like data analysis and machine learning since these values are a good approximation of many real-world phenomena.

Syntax

The randn() function’s syntax is given below:

X = randn
X = randn(n)
X = randn(sz1,...,szN)
X = randn(sz)

Here,

  • The function X = randn yields a randomly selected scalar value from the standard normal distribution.
  • The function X = randn(n) yields a matrix of normally distributed random values having size n-by-n.
  • The function X = randn(sz1,…,szN) yields an array of normally distributed random values having size sz1, sz2,…,szN. Where sz1, sz2…,szN represents the dimension of the array.
  • The function X = randn(sz) returns an array of normally distributed random values having size sz. Where sz is a vector that defines the size of the array.

Examples

Consider some examples to understand the implementation of the randn() function in MATLAB.

Example 1: How to Implement the randn Function in MATLAB?

In this example, we produce a normally distributed random value using the randn function in MATLAB.

X = randn

Example 2: How to Use the randn(n) Function in MATLAB?

This MATLAB code generates a matrix of normally distributed random values having size 4-by-4 using the randn(4) function in MATLAB.

X = randn(4)

Example 3: How to Use randn(sz1,sz2,…,szN) Function in MATLAB?

The given example uses the randn(3,4,2) function for producing an array of normally distributed random values having the size 3-by-4-by-2 in MATLAB.

X = randn(3,4,2)

Example 4: How to Use the randn(sz) Function in MATLAB?

This MATLAB code uses the randn([3,4,2]) function for producing an array of normally distributed random values having size 3-by-4-by-2 in MATLAB.

X = randn([3 4 2])

Conclusion

Random numbers play a vital role in various fields of cryptography and data science. These numbers can be generated easily by using MATLAB’s built-in functions according to their type. The randn() is a built-in function in MATLAB used for generating normally distributed random numbers. This guide has explored the importance of using random numbers and discussed how you can use the randn() function in MATLAB to quickly generate normally distributed random numbers.

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.