# How to generate random numbers between two numbers in JavaScript?

To generate a random number, we use the Math.random() function. This method returns a floating-point number in the range 0 (inclusive) to 1 (exclusive). To generate random numbers in different range, we should define the minimum and maximum limits of the range. Please follow the second syntax below for this.

To generate random integers, you can follow the third and fourth syntax discussed below.

As the Math.random() method gives us a random floating point number so to convert the result into an integer, we need to apply some basic math. Please follow the below syntaxes.

## Syntax

Following is the syntax for generating a random number between two numbers −

• Generate a random number between 0 (inclusive) and 1 (exclusive).

MMath.random()

• Generate a random number between two numbers.

Math.random() * (max-min) + min);

• Generate a random integer between two numbers min and max (both min and max are inclusive).

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

• Generate a random integer between two numbers min and max (the min is inclusive, and the max is exclusive).

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


Here min and max are the start and end numbers between those we want to generate the random numbers.

### Example 1

Generate a random number between 0 (inclusive) and 1 (exclusive).

In the example below, we generate a random number between 0 and 1. Here 0 in inclusive and 1 is exclusive.

<html>
<body>
<h3>Genrate a random number between 0 (inclusinve) and 1 (exclusinve)</h3>
<p id="result"></p>
<script>
var random = Math.random();
document.getElementById("result").innerHTML = random;
</script>
</body>
</html>


### Example 2

Generate many random numbers between 0 (inclusive) and 1 (exclusive).

In the example below, we generate 10 random numbers between 0 and 1. Here 0 is inclusive and 1 is exclusive.

<html>
<body>
<h3>Genrate 10 random numbers between 0 (inclusinve) and 1 (exclusinve)</h3>
<p id="result"></p>
<script>
for(var i =0; i < 10; i++){
var random = Math.random();
document.getElementById("result").innerHTML += random + "<br>";
}
</script>
</body>
</html>


### Example 3

Generate a random number between two numbers.

In the example below, we generate a random number between 20 and 50.

<html>
<body>
<h3>Genrate a random number between 20 and 50</h3>
<p id="result"></p>
<script>
var min = 20;
var max = 50
var random = Math.random() * (max - min) + min;
document.getElementById("result").innerHTML += random ;
</script>
</body>
</html>


### Example 4

Generate many random numbers between two numbers.

In the example below, we generate 10 random numbers between 20 and 50.

<html>
<body>
<h3>Genrate 10 random number between 20 and 50</h3>
<p id="result"></p>
<script>
var min = 20;
var max = 50;
for(var i =0; i < 10; i++){
var random = Math.random() * (max - min) + min;
document.getElementById("result").innerHTML += random + "<br>";
}
</script>
</body>
</html>


Notice that up to now; the generated random numbers are floating point numbers. Now let’s see how to generate random integers.

### Example 5

Generate a random integer between two numbers.

In the below example, we generate a random integer between 10 and 100.

<html>
<body>
<h3>Genrate a random integer between10 and 100</h3>
<p id="result"></p>
<script>
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
document.getElementById("result").innerHTML = randomInt(10,100);
</script>
</body>
</html>


### Example 6

In the below example, we take the two numbers from the user. We generate a random number between these two numbers.

<html>
<body>
<h3>Generate Random Number Using JavaScript</h3>
<div style="display: inline-block;">Random Number Between <input type="text" class="start"> and <input type="text" class="end"></div> <br /><br />
<input type="button" value="Get Number" onclick="getRandomNumber();">
<p class="number"></p>
<script>
function getRandom(start,end){
return Math.floor(Math.random()*(end-start+1))+start;
}
function getRandomNumber(){
var start = document.getElementsByClassName('start')[0].value;
var end = document.getElementsByClassName('end')[0].value;
document.getElementsByClassName('number')[0].innerHTML = 'The Random Number is : <span class="highlight">'+getRandom(parseInt(start),parseInt(end))+'</span>';
}
</script>
</body>
</html>


### Example 7

Generate many random integers between two numbers.

In the below example, we generate 10 random integers between 10 and 100.

<html>
<body>
<h3>Generate 10 random integers between 10 and 100</h3>
<p id="result"></p>
<script>
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
for (var i = 0; i < 10; i++) {
document.getElementById("result").innerHTML += randomInt(10, 100) + "<br>";
}
</script>
</body>
</html>


In this tutorial, we discussed ways to generate random numbers between two numbers. We saw how to generate a single as well as many random numbers between two numbers. We generated random numbers as floating-point and integers.

Updated on: 05-Sep-2022

10K+ Views