JavaScript Math.random() to Generate an Integer in a Range
For a random integer from min through max, use Math.floor(Math.random() * (max - min + 1)) + min. With 1 and 6, the result is one of the six values on a die.
What Math.random() returns
Math.random() returns a pseudorandom number from 0 inclusive to 1 exclusive, with an approximately uniform distribution. Successive values can repeat, and the standard API has no seed parameter. ECMAScript · Math.random()
const value = Math.random();
console.log(value >= 0 && value < 1); // true
Include both endpoints in an integer range
There are six integers from 1 to 6. Multiply by 6 and round down to obtain 0 through 5, then add 1. The range from -2 to 2 likewise has five slots. Using Math.round() to scale to the endpoints gives them narrower intervals than interior values.
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randomInteger(1, 6));
console.log(randomInteger(-2, 2));
console.log(randomInteger(4, 4)); // 4
The short function below assumes small integer bounds. Filter out fractions and very large values first. With large integers, Number precision requires particular care around the span and endpoints.
Try the bounds in a complete example
Save the code as random-integer.html in UTF-8 and open it in a browser. Enter integers from -1,000 through 1,000. Blank entries, fractions, and reversed bounds are rejected. With the defaults 1 and 6, the result is in that range.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Draw an integer in a range</title>
</head>
<body>
<label for="minimum">Minimum</label>
<input id="minimum" type="number" min="-1000" max="1000" step="1" value="1">
<label for="maximum">Maximum</label>
<input id="maximum" type="number" min="-1000" max="1000" step="1" value="6">
<button id="draw" type="button">Draw integer</button>
<p id="result" aria-live="polite"></p>
<script>
const minimum = document.querySelector('#minimum');
const maximum = document.querySelector('#maximum');
const result = document.querySelector('#result');
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
document.querySelector('#draw').addEventListener('click', () => {
const min = minimum.valueAsNumber;
const max = maximum.valueAsNumber;
if (!Number.isInteger(min) || !Number.isInteger(max) ||
min < -1000 || max > 1000 || min > max) {
result.textContent = "Enter integers from -1,000 to 1,000 in ascending order.";
return;
}
result.textContent = `Drawn value: ${randomInteger(min, max)}`;
});
</script>
</body>
</html>
Set both endpoints to 4 to get 4 every time. With -2 and 2, negative results and zero are possible. Repeated clicks do not guarantee that each possible value appears.
Use a separate API for security-sensitive randomness
For values that must be unpredictable, such as password reset tokens, use a cryptographic random number generator. The browser’s Web Crypto API provides crypto.getRandomValues(). Web Cryptography · getRandomValues()




