Matlab

What Does rand(n,1) Do in Matlab?

MATLAB offers users with multiple functions to generate a scalar, a vector, a matrix, or an array of random numbers. These functions will quickly generate different random numbers in different distributions according to their functionalities. One function among these is the rand(n,1) function which allows us to generate a column vector of uniformly distributed random numbers.

In this article, we will learn how to generate random numbers using the rand(n,1) function in MATLAB using some examples.

How to Use the rand(n,1) Function in MATLAB?

The rand(n,1) is a function in MATLAB that allows us to generate a column vector of uniformly distributed random numbers between 0 and 1. This function is a rand() function that can generate a scalar, a vector, or a matrix with uniform distribution having all entries lying between 0 and 1. However, this function only generates a column vector having n rows.

A simple syntax followed by this function is given below:

X = rand(n,1)

Here.

X = rand(n,1) yields an n-by-1 vector of uniformly distributed randomly generated numbers having all entries lying between 0 and 1.

Example 1

In this MATLAB code, we generate a 4-by-1 column vector of random numbers that lie between 0 and 1 using the rand(n,1) function by considering n = 4.

rand(4,1)

A column vector of random numbers that is an output of the above code is displayed below.

Example 2

The following example shows the difference between generating random numbers using the rand(5,1) and simple rand() function.

a=rand(5,1)

b=rand()

Conclusion

The rand(n,1) is a function used in MATLAB for generating a column vector of uniformly distributed random numbers lying between the range (0,1). This function behaves like a simple rand() function having the difference that it only generates a column vector; however, the rand() function can only generate a single random number. This tutorial provided an easy guide to understanding the working of the rand(n,1) function in generating random values.

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.