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 Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random ...
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
Multiplying by 10 gives a number from 0 up to but not including 10. Adding 1 shifts that range to 1 up to but not including 11. Math.floor() then rounds down, so you get an integer between 1 and 10. // Returns a random integer from 1 to 100 (both included): Math.floor(Math.random() * 100) + 1; Try it Yourself »
🌐
CoreUI
coreui.io › blog › how-to-generate-a-random-number-in-javascript
Javascript Random - How to Generate a Random Number in JavaScript? · CoreUI
April 16, 2024 - Math.random() is a powerful tool in JavaScript that generates a pseudo-random number—a number that seems random but is actually generated through a deterministic process. It returns a floating-point, or decimal, number between 0 (inclusive) and 1 (exclusive).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-generate-random-number-in-given-range-using-javascript
Generate Random Number in Given Range Using JavaScript - GeeksforGeeks
July 11, 2025 - Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive). Multiply the result by (max - min + 1) to scale it to the desired range, then add min to shift it to the correct starting value.
🌐
GitHub
gist.github.com › kerimdzhanov › 7529623
JavaScript: get a random number from a specific range · GitHub
I want to print random number between 70 t0 100.. ... that's not working ... what is getRandomInt? there is no any predefined function of getRandomInt.. i tried Math.floor(Math.random*100); but that wat i received 1 to 100 but I just need 70 to 100.. ... const getRandomFloat = (min, max) => Math.random() * (max - min) + min; const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); const getRandomBool = () => Math.random() >= 0.5;
🌐
KIRUPA
kirupa.com › html5 › random_numbers_js.htm
Random Numbers in JavaScript
// Random number between 0 and ... is a function you can use instead: function getRandomNumber(low, high) { let r = Math.floor(Math.random() * (high - low + 1)) + low; return r; }...
🌐
SheCodes
shecodes.io › athena › 54932-how-to-generate-a-random-number-between-1-to-6-in-javascript-using-math
[JavaScript] - How to generate a random number between 1 to 6 in JavaScript using Math
Fall In Love With Coding Sale 🎉 30% off on all coding workshops ending on February 16th Ending in 5 days Get Deal Get This Deal NOW ... Learn how to use Math.random() and Math.floor() methods in JavaScript to generate a random number between 1 to 6 ... How can I get number from 1 to 6 in JS using Math.
Find elsewhere
🌐
W3docs
w3docs.com › javascript
How to Generate a Random Number Between Two Numbers in JavaScript
In this example, 1 is considered ... + start (5) - end (1)). Math.random() is used for returning return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive)....
🌐
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):
🌐
Reddit
reddit.com › r/learnprogramming › generate a random number within a range - how does the math work?
r/learnprogramming on Reddit: Generate A Random Number Within A Range - How Does the Math Work?
February 4, 2022 -

Hi all,

I'm learning JS through FCC and it's going okay so far. I'm not a very mathematically inclined person, however, so I struggle with some concepts.

I'm struggling to understand how the following code works;

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

I tried writing out an example on paper and used 10 and 5 as max and min respectively.

Math.floor(Math.random() * (10 - 5 + 1)) + 5

wouldn't this mean that I can get numbers higher than the max though? Say I get a randomly generated 0.99; well 0.99 * (6) + 5 = 10.94 which is greater than my max of 10?

Am I doing this wrong?

🌐
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);
🌐
Team Treehouse
teamtreehouse.com › community › javascript-random-number-generator
JavaScript Random Number Generator (Example) | Treehouse Community
April 27, 2016 - Math.floor(Math.random() * 6) returns a whole number between 0 and 5 · If we want a number between 0 and 6 we can add 1 to the result
🌐
Medium
medium.com › @ryan_forrester_ › random-number-in-a-range-in-javascript-464dfeddf8d4
Random Number in a Range in JavaScript | by ryan | Medium
September 11, 2024 - Here’s how you can do that: function ...s.push(getRandomNumberInRange(min, max)); } return randomNumbers; } const randomNumbers = getRandomNumbersArray(1, 10, 5); console.log(randomNumbers); // Output: an array of 5 random integers ...
🌐
TutorialsPoint
tutorialspoint.com › How-to-generate-random-numbers-between-two-numbers-in-JavaScript
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.
🌐
Medium
josephcardillo.medium.com › using-math-random-in-javascript-c49eff920b11
Using Math.random() in JavaScript - Joe Cardillo - Medium
January 31, 2022 - Using Math.random() in JavaScript A short guide to .ceil, .floor, and .round In JavaScript, to get a random number between 0 and 1, use the Math.random() …
🌐
Programiz
programiz.com › javascript › examples › random-between-numbers
Javascript Program to Generate a Random Number Between Two Numbers
Math.random() returns a random floating-point number ranging from 0 to less than 1 (inclusive of 0 and exclusive of 1)
🌐
Udacity
udacity.com › blog › 2021 › 04 › javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - In the previous examples, Math.random() ... but never 5. The solution to this problem, if you’re creating an integer, is adding 1 to the result....
🌐
CapCut
capcut.com › explore › javascript-random-number-between-1-and-5
JavaScript Random Number Between 1 and 5: Create Efficient Code | CapCut - AI Tools
Learn how to generate a JavaScript random number between 1 and 5 with simple, effective methods tailored for developers and beginners alike. Discover clear code examples that you can easily integrate into your projects, whether building games, quizzes, or dynamic web applications.