JavaScript

How to Generate a Random Number Between Two Numbers in JavaScript

The extraction of a single number using any mathematical algorithm is known as random number generation. The random number is an unexpected or unpredictable value generated by a computer. Many applications need to create random numbers, such as gaming websites, testing purposes, and so on. Some applications generate random numbers to utilize the OTP to validate the user, like banking apps and so on.

This blog post will define methods for creating random numbers between two numbers.

How to Generate a Random Number Between Two Numbers in JavaScript?

In JavaScript, there are different methods for generating a random number, including:

  • “Math.random()” method 
  • “Math.floor()” method

Let’s discuss these methods one by one!

Method 1: Generate a Random Number Between Two Numbers Using Math.random() Method

The “Math.random()” method is used for creating random numbers between two numbers. It generates random positive double data type values between “0.0” and “1.0”.

Syntax

The below-given syntax can generate a random number using the “Math.random()” method:

Math.random();

For generating a random number between two numbers, utilize the given syntax:

Math.random() * (max_num - min_num) + min_num;

In the above syntax, “max_num” represents the maximum number, and “min_num” denotes the minimum number.

Return Value

It outputs random pseudo numbers rather than true values inside the range.

Example

Let’s generate a random number between “83” and “23” using the “random()” method:

Math.random() * (83-23) + 23;

Output

The above output displays a floating-point random number “48.04761034288709” between two numbers “83” and “23”.

If the user wants to get the random number in an integer format! Follow the below section.

Method 2: Generate a Random Number Between Two Numbers Using Math.floor() Method

Another method for generating random numbers between two numbers, use the “Math.floor()” method with the “Math.random()” method. It will convert the randomly generated floating point number to an integer.

Syntax

Follow the given-provided syntax of the “Math.floor()” method to generate an integer random number:

Math.floor(Math.random() * (max_num - min_num + 1) + min_num);

The above syntax:

  • First, call the “Math.random()” method for generating a random number.
  • Then, convert it to an integer by rounding off the returned floating-point number by the “Math.random()” method.

Example

Let’s generate a random number between two numbers, “55” and “20”, using the “Math.floor()” method:

Math.floor(Math.random() * (55 - 20 + 1) + 20);

Output

The above output indicates that the “Math.floor()” method gives an integer random number between two numbers.

Conclusion

To generate a random number between two numbers, use the “random()” method of the Math object that will generate a decimal random between two specified numbers, and for converting it to an integer number, use the “Math.floor()” method with random() method. This post defines these methods with proper examples for creating random numbers between two numbers.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.