Javascript Program to Generate a Random Number Between Two Numbers

In this example, you will learn to write a JavaScript program that will generate a random number between two numbers.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


If you want to find the random integer in between min(inclusive) to max(inclusive), you can use the following formula:

Math.floor(Math.random() * (max - min + 1)) + min

Example: Integer value between two numbers

// input from the user
let min = parseInt(prompt("Enter a min value: "));
let max = parseInt(prompt("Enter a max value: "));

// generating a random number
let a = Math.floor(Math.random() * (max - min + 1)) + min;

// display a random number
console.log(`Random value between ${min} and ${max} is ${a}`);

Output

Enter a min value: 1
Enter a min value: 50
Random value between 1 and 50 is 47

In JavaScript, you can generate a random number with the Math.random() function.

Math.random() returns a random floating-point number ranging from 0 to less than 1 (inclusive of 0 and exclusive of 1)

The above program will show an integer output between min(inclusive) to max(inclusive).

First, the minimum and maximum value is taken as input from the user. Then the Math.random() method is used to get the random number from the passed value.

The Math.floor() returns the nearest integer value.