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
🌐
W3Schools
w3schools.com › js › js_random.asp
W3Schools.com
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 »
Discussions

Generate A Random Number Within A Range - How Does the Math Work?
Math.floor() is the floor function. Which means it returns the integer part of a number. In your example Math.floor(5.94) = 5 More on reddit.com
🌐 r/learnprogramming
8
0
February 4, 2022
Basic JavaScript - Generate Random Whole Numbers with JavaScript
Tell us what’s happening: The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9 When I return Math.floor(Math.random()*9) it is not accepted. The error says " You should have multiplied the result of ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
April 18, 2023
Random Number Generation
Hi all. I just started here with the JS curriculum. When I did the first "game"ish exercise, WordBlanks, I thought it needed a little more oomph. So I added a Random Number Generator using Math.random. I threw in Math.round and a mulltiplier to give me random whole numbers between 0-9 for array ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
June 22, 2021
Get a number between 1 and 10 with math.random
I don’t really know what this is called or how to explain it but I have a math.random that’s 1-100 and when a number is picked I want it to print something if its between 1-10, how do i do that? The method im using right now is really messy and not optimized, local a = math.random(1,100) ... More on devforum.roblox.com
🌐 devforum.roblox.com
1
0
February 24, 2022
People also ask

What is the default range of numbers generated by Math.random() in JavaScript?
Math.random() generates a floating-point, pseudo-random number in the range from 0 (inclusive) to 1 (exclusive). This means you'll get numbers like 0.5, 0.1234, but never 1.
🌐
wscubetech.com
wscubetech.com › resources › javascript › programs › random-number
Generate Random Number in JavaScript (7 Programs)
Why can’t I set a seed for Math.random() in JavaScript?
JavaScript does not provide a built-in method to set a seed for its random number generator. This is by design, as Math.random() is meant to provide quick and basic pseudo-random numbers without the complexity of seed management.
🌐
wscubetech.com
wscubetech.com › resources › javascript › programs › random-number
Generate Random Number in JavaScript (7 Programs)
How do I create a random string of characters in JavaScript?
To create a random string, you can first define a string of all possible characters, and then repeatedly use Math.random() to pick a character from this string until you reach your desired length.
🌐
wscubetech.com
wscubetech.com › resources › javascript › programs › random-number
Generate Random Number in JavaScript (7 Programs)
🌐
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 ... A floating-point, pseudo-random number ...
🌐
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?

🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › appendices › math-method-examples › random-examples.html
Math.random Examples
floor = 0, ceil = 1, round = 0 ... Math.random() by 10, applying the floor method gives numbers between 0 and 9. Using the ceil method shifts the range up one digit, generating values between 1 and 10....
🌐
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 - function getRandomNumberInRange(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } const randomNum = getRandomNumberInRange(1, 10); console.log(randomNum); // Output: a random integer between 1 and 10
Find elsewhere
🌐
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).
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Generate Random Whole Numbers with JavaScript - JavaScript - The freeCodeCamp Forum
April 18, 2023 - Tell us what’s happening: The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9 When I return Math.floor(Math.random()*9) it is…
🌐
WsCube Tech
wscubetech.com › resources › javascript › programs › random-number
Generate Random Number in JavaScript (7 Programs)
January 22, 2026 - Looking for ways to generate random numbers in JavaScript? Our comprehensive guide features 7 programs that will help you generate random numbers.
🌐
Medium
josephcardillo.medium.com › using-math-random-in-javascript-c49eff920b11
Using Math.random() in JavaScript - Joe Cardillo - Medium
January 31, 2022 - If you want a random number between 1 and 10, multiply the results of Math.random by 10, then round up or down.
🌐
SitePoint
sitepoint.com › blog › javascript › how to generate random numbers in javascript with math.random()
Generating Random Numbers in JavaScript with Math.random()
November 7, 2024 - To use it, simply enter Math.random() ... want a random number that’s bigger than 1? Easy: all you need to do is multiply by a scale factor to scale it up — for example, multiplying the result by 10 will produce a value between ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Random Number Generation
June 22, 2021 - Hi all. I just started here with the JS curriculum. When I did the first "game"ish exercise, WordBlanks, I thought it needed a little more oomph. So I added a Random Number Generator using Math.random. I threw in Math.round and a mulltiplier to give me random whole numbers between 0-9 for array picking.
🌐
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
/* Defining a custom function which ... } // Generate a random number between 1 and 10 (including 1 and 10) var randomNum = generateRandomNumber(1, 10); console.log(randomNum);...
🌐
Keploy
keploy.io › home › community › how to generate random numbers in javascript
How to Generate Random Numbers in JavaScript | Keploy Blog
November 1, 2024 - Learn to generate random numbers, integers, Booleans in JavaScript for different scenarios, from basic to advanced techniques.
🌐
Sololearn
sololearn.com › en › Discuss › 2701557 › how-to-generate-random-number-between-10-1000
How to generate random number between 10 & 1000?? | Sololearn: Learn to code for FREE!
In javascript : Math.floor(Math.random()*990) + 10; Here, 1. Math.random() will generate a number between 0 to 1. 2. Multiplying the number by 990 times will be a number between 0 to 990.
🌐
Codecademy
codecademy.com › forum_questions › 4f2ab50106bc4500010080ef
Why I can't use Math.random()*11 instead ...
And when I use Math.random in conjunction with Math.floor the value 10.9999 is round down to 10. ... Math.random() gives numbers between 0 and 1, but NOT 1. So Math.random()*11 will give numbers between 0 and 11, but not 11.
🌐
Josh W. Comeau
joshwcomeau.com › snippets › javascript › random
Generate a random number in a range in JavaScript • Josh W. Comeau
May 17, 2020 - // Get a random number out of [10, 11, 12, 13] random(10, 14); // Get a random number from 1 to 100 (inclusive) random(1, 101); // Get a random number from -10 to 10 (inclusive) random(-10, 11); This function maps the 0..1 range generated by ...
🌐
Codecademy
codecademy.com › forum_questions › 5020be4d3a51800002015ebe
Math.floor and Math.random | Codecademy
Math.random() produces a random ... 0.999999 (etc). This is a really important fact. It means that Math.random() * 10 can never actually get you a 10....