Matlab

How to Generate Random Numbers Between Two Bounds 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. Among these is the rand() function, which allows us to generate uniformly distributed random numbers lying between 0 and 1. In this guide, we are going to explore how to create random numbers between two bounds using rand() function in MATLAB.

How to Create Random Numbers Between Two Bounds in MATLAB?

As we know the rand() function by default generates random numbers in the bounds (0,1) but this function can generate random numbers between any two bounds specified by the user by performing some basic mathematical operations. This function can be used for generating a vector, a scalar, or, a matrix of random numbers between two bounds. This function follows a simple syntax that is given below:

X = (b - a).*rand +a

X = (b - a).*rand(n) +a

X = (b - a).*rand(sz1, sz2,…,szN) +a

Here:

X = (b – a).*rand +a returns a random scalar selected from the uniform distribution lying between specified bounds a and b.

X = (b – a).*rand(n) +a yields an n-by-n matrix of randomly generated numbers with a uniform distribution having all entries lying between specified bounds a and b.

X = (b – a).*rand(sz1, sz2,…,szN) +a returns a random number array with uniform distribution having all entries lying between specified bounds a and b and having a size of sz1 by… by szN where sz1,…,szN denote the dimensions’ sizes.

Consider some examples that demonstrate how to generate random numbers between two bounds in MATLAB.

Example 1

The given example generates a scalar random number that lies between specified bounds a = 5 and b = 10 using rand() function.

a = 5;

b = 10;

r = (b-a).*rand + a

Example 2

In this example, we generate a 3-by-3 matrix of random numbers having all entries lying between specified bounds a = 5 and b = 10 using the rand(n) function. Here, we consider n = 3.

a = 5;

b = 10;

r = (b-a).*rand(3) + a

Example 3

In this MATLAB code, we generate a 3-by-4 matrix of random numbers having all entries lying between specified bounds a = 5 and b = 10 using the rand(sz1,sz2) function by considering sz1 = 3 and sz2 = 4.

a = 5;

b = 10;

r = (b-a).*rand(3, 4) + a

Conclusion

The rand() is a MATLAB built-in function that is used for generating uniformly distributed random numbers lying between the range (0,1) by default. But we can use this function to generate random numbers lying between two specified bounds by performing some basic mathematical operations. This tutorial taught us how to generate random numbers between two bounds using the rand() function.

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.