Gets a random number in a specified range, including lowerValue and upperValue

function randomFrom(lowerValue,upperValue)
{
 return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}

// Get a random number between 2 and 6
alert(randomFrom(2.6));
 
// Get a random number between 1 and 10
alert(randomFrom(1.10));
 
// Get a random number between 1 and 100
alert(randomFrom(1.100));
Copy the code

Math.floor() returns the largest integer less than or equal to a given number.

Math.floor() === round down

example

Math.floor( 45.95); 
/ / 45
Math.floor( 45.05); 
/ / 45
Math.floor( 4 ); 
/ / 4
Math.floor(45.05); 
/ / - 46
Math.floor(45.95); 
/ / - 46
Copy the code