Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Generate random numbers using ↴
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.
Math.floor() static method → always rounds down and returns the largest integer less than or equal to a given number.
Math.ceil() static method → always rounds up and returns the smallest integer greater than or equal to a given number.
Math.random() static method returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
Math.random(); → 0.7409774889800926
Math.random(); → 0.5533596609238658
Math.random(); → 0.26768267226253617
Returns random numbers between 0 (inclusive) and 1 (exclusive)
To get a random number between two values.
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
getRandom(1, 6); → 5.075061196157429
getRandom(1, 6); → 3.452911945928266
getRandom(1, 6); → 2.764107420388978
Returns random numbers between 1 (inclusive) and 6 (exclusive)
To get a random integer number between two values use the Math.floor method.
function getRandom2(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
getRandom2(1, 6); → 5
getRandom2(1, 6); → 3
getRandom2(1, 6); → 2
returns random integer between 1 (inclusive) and 6 (exclusive)
To get an inclusive random integer between two values.
function getRandom3(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
getRandom3(1, 6); → 1
getRandom3(1, 6); → 3
getRandom3(1, 6); → 6
Returns random integer between 1 (inclusive) and 6 (inclusive)
Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
Math.floor(5.25); → 5
Math.floor(2.99); → 2
Math.floor(6.55); → 6
rounds down and returns the largest integer less than or equal to a given number.
Math.ceil() static method always rounds up and returns the smallest integer greater than or equal to a given number.
Math.ceil(5.25); → 6
Math.ceil(2.99); → 3
Math.ceil(6.55); → 7
rounds up and returns the smallest integer greater than or equal to a given number.
Initialize a variable to hold the minimum number in the range.
const num1 = 1; → user input
Initialize a variable to hold the maximum number in the range.
const num2 = 10; → user input
Define a function randomNum to generate a random integer within a given range.
function randomNum(str) {}
The function takes two numbers as input num1, num2 and returns a random integer within a given range.
Round up the minimum value to the nearest integer.
const minCeiled = Math.ceil(min) minCeiled
Round down the maximum value to the nearest integer.
const maxFloored = Math.floor(max) maxFloored
Generate a random integer between minCeiled and maxFloored
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
Call the function with ↴
randomNum(num1, num2);
Generate random numbers.
const num1 = 1;
const num2 = 10;
function randomNum(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
}
call function
randomNum(num1, num2); → 5
randomNum(num1, num2); → 7
randomNum(num1, num2); → 3
Function returns a random number after each call.
Alternative ↴
Math.floor(Math.random() * (max - min + 1)) + min;