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
function getRandomInt(max) { return Math.floor(Math.random() * max); } console.log(getRandomInt(3)); // Expected output: 0, 1 or 2 console.log(getRandomInt(1)); // Expected output: 0 console.log(Math.random()); // Expected output: a number from 0 to <1 · js · Math.random() None.
🌐
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 »
🌐
Josh W. Comeau
joshwcomeau.com › snippets › javascript › random
Generate a random number in a range in JavaScript • Josh W. Comeau
What if we want a minimum other than 0? What if we want a value between 1 and 4? The trick is that we need to get the delta. 4 - 1 is 3, so we'll get a random value between 0 and 2.99999.
🌐
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).
🌐
The Valley of Code
thevalleyofcode.com › how-to-generate-random-number-between
How to generate a random number between two numbers in JavaScript
Use a combination of Math.floor() and Math.random(). This simple one line of code will return you a number between 1 and 6 (both included): ... There are 6 possible outcomes here: 1, 2, 3, 4, 5, 6.
Find elsewhere
🌐
Udacity
udacity.com › blog › 2021 › 04 › javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - Javascript creates pseudo-random numbers with the function Math.random(). This function takes no parameters and creates a random decimal number between 0 and 1.
🌐
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
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.
🌐
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)
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-generate-a-random-number-between-two-numbers-in-javascript.php
How to Generate a Random Number between Two Numbers in JavaScript
The Math.random() method returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1), whereas the Math.floor() method round the number down to the nearest integer. Let's take a look at the following example to see how it works: ... /* Defining a custom function which returns a random number between min and max, including min and max */ function generateRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } // Generate a random number between 1 and 10 (including 1 and 10) var randomNum = generateRandomNumber(1, 10); ...
🌐
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() …
🌐
W3Schools
w3schools.com › jsref › jsref_random.asp
JavaScript Math random() Method
cssText getPropertyPriority() ... » · A random whole number between 1 and 10 (inclusive): let x = Math.floor((Math.random() * 10) + 1); Try it Yourself » ·...
🌐
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):
🌐
Stack Overflow
stackoverflow.com › questions › 57063216 › need-to-generate-random-number-between-1-3-and-need-to-set-3-variables
Need To Generate Random Number Between 1 - 3 And Need To Set 3 Variables
//Define Min Number Range & Main Number & Random Number var Min = 10; var Max = 40; var S1 = Math.floor(Math.random() * (Max - Min + 1) + Min); var S2 = Math.floor(Math.random() * (Max - Min + 1) + Min); var MainNumber = S1 + S2; var RandomNumber = Math.floor(Math.random() * (Max - Min + 1) + Min); console.log('S1 = ', S1); console.log('S2 = ', S2); console.log('Random = ', RandomNumber); console.log('Main = ', MainNumber); //Assign Numbers To Elements On HTML //String Conv document.getElementById("p1").innerHTML = S1; document.getElementById("p2").innerHTML = S2; document.getElementById("p3")
🌐
GitHub
gist.github.com › kerimdzhanov › 7529623
JavaScript: get a random number from a specific range · GitHub
/** * Get a random floating point number between `min` and `max`. * * @param {number} min - min number * @param {number} max - max number * @return {number} a random floating point number */ let randomFloat = (min, max) => Math.random() * (max - min) + min; /** * Get a random integer between `min` and `max`. * * @param {number} min - min number * @param {number} max - max number * @return {number} a random integer */ let randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); /** * Get a random boolean value. * * @return {boolean} a random true/false */ let randomBool = () => Math.random() >= 0.5; ... If it's okay with you, I'm going to use the code of random.js in a javascript extension I am working on (masmas.js)
🌐
YouTube
youtube.com › watch
How to Generate Random Numbers in JavaScript | Math.random() Explained (Tutorial) - YouTube
🎲 Master Generating Random Numbers in JavaScript | Complete Guide to Math.random() (2025)Want to add randomness to your JavaScript programs? Whether it's fo...
Published   June 19, 2025
🌐
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?