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
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 ...
🌐
Vultr Docs
docs.vultr.com › javascript › examples › generate-a-random-number-between-two-numbers
JavaScript Program to Generate a Random Number Between Two Numbers | Vultr Docs
December 18, 2024 - In this article, you will learn how to craft a JavaScript function to generate a random number between two specified numbers.
🌐
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
You can simply use the Math.random() method in combination with the Math.floor() method to generate a random number between two numbers in JavaScript.
🌐
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.
Find elsewhere
🌐
The Valley of Code
thevalleyofcode.com › how-to-generate-random-number-between
How to generate a random number between two numbers in JavaScript
How to generate a random number between two numbers in JavaScript · The simplest possible way to randomly pick a number between two · Use a combination of Math.floor() and Math.random(). This simple one line of code will return you a number ...
🌐
GitHub
gist.github.com › brunomonteiro3 › 27af6d18c2b0926cdd124220f83c474d
Generate random number between two numbers in JavaScript · GitHub
Again, you don't need to check this function, because you pass the min/max. On the mozilla guide, for the Math.random, you can find other random generator's. ... function Numbers(min,max){ console.log(Math.floor(Math.random()*(max-min+1)+min)); } Numbers(15, 20)
🌐
W3docs
w3docs.com › javascript
How to Generate a Random Number Between Two Numbers in JavaScript
Math.random() is used for returning return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive).
🌐
Javascripts
javascripts.com › generate-random-numbers-between-two-numbers-in-javascript
How to Generate Random Numbers Between Two Numbers in JavaScript
June 23, 2023 - This is done by utilizing the built-in Math.random() method, which generates a random floating-point number between 0 and 1. After getting this random number, we scale it up to our desired range, and then shift it to start from our lower bound.
🌐
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):
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-math-random-method
JavaScript Math random() Method - GeeksforGeeks
July 15, 2024 - Example 2: Math. random() can be used to get a random number between two values. The returned value is no lower than min and may possibly be equal to min, and it is also less than and not equal to max.
🌐
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
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript random number between
How to Generate a Random Number Between Two Numbers in JavaScript | Delft Stack
February 2, 2024 - If we also want to have a user-defined minimum value, we need to change the equation of Math.random() * max to Math.random() * (max-min)) +min. Using this equation the returned value is a random number between min and max.
🌐
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.
🌐
Latenode
community.latenode.com › other questions › javascript
Create a random number between two specific values in JavaScript - JavaScript - Latenode Official Community
October 6, 2024 - How can I produce a random number within a given range using JavaScript? For instance: if the range is 1 to 6, the random number could be 1, 2, 3, 4, 5, or 6.