JavaScript

JavaScript Math random() Method | Explained

In JavaScript, there are multiple built-in methods that are used to achieve different functionalities, for example, the Math.round() method rounds the number to the nearest integer, the Math.trunc() method keeps the integer value and skips the fractional part of the given value, the Math.random() function returns a random numeric value between zero(included) and one (not included), etc.

This write-up will explain the thorough understanding of the below-listed aspects of Math.random() method:

So, let’s start!

What is Math.random()

It is a predefined method used to create a random floating point(fractional) number between 0.0(included) and 1.0(excluded).

In JavaScript, by default the Math.random() produces a random value between (0, 1). However, we can specify the range of our choice by multiplying the returned value of the Math.random() method to the magnitude of the range.

Basic Syntax

The below snippet shows the basic syntax to generate a random number using Math.random() method in JavaScript:

Math.random();

The above snippet shows that the Math.random() method doesn’t take any parameter.

How Math.random() Method works in JavaScript

In this section, we will understand all the essentials of the Math.random() method in JavaScript. To do so, we will consider some examples.

How to use the Math.random() method to get a random number

In this example we will utilize the Math.random() method to get a random number:

let number = Math.random();

console.log("Randomly Generated Number: ", number);

Whenever we run this program, we will get a new random number between 0.0(inclusive) and 1.0 (exclusive):

The output shows that the Math.random() method returned a random number.

How to generate a random number (floating point number) between the user-specified range

This time the Math.random() will create a random number between the user-specified range:

function randomValue(minVal, maxVal) {

return Math.random() * (maxVal - minVal) + minVal;

}

console.log("Random Value between 5 and 10: ", randomValue(5,10));

The output shows that this Math.random() method produced a random number between the user-specified range.

How to generate an integer value using Math.random() method in JavaScript

To generate random integer values in JavaScript, we can use various Math methods like round(), ceil(), etc. along with the Math.random() method.

var number = Math.round(Math.random()*10);

console.log("Random Value: ", number);

In this example, we utilized the Math.round() with Math.random() method. Consequently, we will get a random number greater than or equal to zero but less than 10:

This is how we can generate a random integer value using the Math.random() method in JavaScript.

How to use Math.random() method to generate an integer value between the user-specified range

In this example, we utilized the Math.floor() method along with the Math.random() method to generate a random integer between 5 and 10:

function randomVal(minVal, maxVal) {

return Math.floor(Math.random() * (maxVal - minVal)) + minVal;

}

console.log("Resultant Value: " , randomVal(5, 15));

The above snippet verifies the working of Math.random() method.

Conclusion

In JavaScript, a predefined method named Math.random() is used to produce a pseudo random fractional/floating point numeric value between 0.0(included) and 1.0(excluded). We can get an integer value using the Math.random() method, to do so, we have to utilize some other Math methods along with the Math.random() method such as Math.round(), Math.ceil(), and so on. This write-up discussed how to get the floating-point random numbers or integer numbers using Math.random() method in JavaScript.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.