function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
const rndInt = randomIntFromInterval(1, 6);
console.log(rndInt);
What it does "extra" is it allows random intervals that do not start with 1. So you can get a random number from 10 to 15 for example. Flexibility.
Answer from Francisc on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
Top answer 1 of 16
3447
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
const rndInt = randomIntFromInterval(1, 6);
console.log(rndInt);
What it does "extra" is it allows random intervals that do not start with 1. So you can get a random number from 10 to 15 for example. Flexibility.
2 of 16
2535
Important
The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:
const rndInt = Math.floor(Math.random() * 6) + 1
console.log(rndInt)
Where:
- 1 is the start number
- 6 is the number of possible results (1 + start (6) - end (1))
Videos
13:48
How to Generate Random Numbers in JavaScript | Math.random() ...
05:24
Math Methods and How to Generate a Random Number with ...
08:36
Random number generator in JavaScript ⁉ - YouTube
Generate random number between two numbers in JavaScript - Stack ...
12:44
Javascript Tutorial to Generate Random Number Between any Two Numbers ...
- YouTube
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
JS Examples JS HTML DOM JS HTML ... a random number: Math.random(); Try it Yourself » · Math.random() returns a random number between 0 (inclusive), and 1 (exclusive): Math.random() always returns a number lower than ...
Programiz
programiz.com › javascript › examples › random-between-numbers
Javascript Program to Generate a Random Number Between Two Numbers
If you want to find a random integer ... ")); const max = parseInt(prompt("Enter a max value: ")); // generating a random number const a = Math.floor(Math.random() * (max - min + 1)) + min; // display a random number console.log(`Random value between ${min} and ${max} is ...
W3Schools
w3schools.com › howto › howto_js_random_number_two_nums.asp
How To Generate a Random Number Between Two Numbers
This JavaScript function always returns a random number between min and max (both included): function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; } Try it Yourself » · Read more about Random Numbers in our JavaScript Random Tutorial.
Codecademy
codecademy.com › forum_questions › 5198adbdbbeddf9726000700
How to get a random number between X(any number) and X(another number)? | Codecademy
// x = -2, y = -10 // note: no need to +1 since Math.floor() rounds down Math.floor(Math.random() * (-10-(-2)) + (-2)); Math.floor(Math.random() * -8 - 2); ... Thanxx.. It helped alot. But can you please explain reason behind (y-x) more for easy understanding? ... Please….I am a beginner programmer and don’t know how to do this. I need a script for a number between o and 0.33.
Medium
medium.com › codimis › how-to-generate-random-integer-between-two-integers-in-javascript-6dabcebcf3c3
How to Generate Random Integer Between Two Integers in Javascript
March 22, 2024 - Let’s say you want to generate ... (max): function getRandomInt(min, max) { // Use Math.floor to round down to the nearest whole number // Use Math.random() to generate a random decimal between 0 (inclusive) and 1 (exclusive) // Multiply by the range (max - min + 1) to cover the entire range // Add the minimum value to shift the range to [min, max] return Math.floor(Math.random() * (max - min + 1)) + min; ...
Educative
educative.io › answers › how-to-generate-a-random-number-between-a-range-in-javascript
How to generate a random number between a range in JavaScript
In JavaScript, the Math.random function will generate a random floating point number between 0 and 1(0 is included 1 is excluded):
WPShout
wpshout.com › home › get random number with javascript
Get Random Number with JavaScript - WPShout
September 5, 2025 - function RandomInteger (min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } console.log(RandomInteger(1, 30)); Code language: JavaScript (javascript) The above code is incorporated into the following interactive CodePen demo.
Address 20 Povernei Street, 4th Floor, Flat no. 9, 010641, Bucharest
Futurestud.io
futurestud.io › tutorials › generate-a-random-number-in-range-with-javascript-node-js
Generate a Random Number in Range With JavaScript/Node.js
This between method accepts two arguments: min and max. Calculate a random number between the min and max values like this:use Math.random() to generate a random number, multiply this random number with the difference of min and max and ultimately add min to it.