# Simpler way for random numbers

Is there an easier way to make a random number between 1 and 10 then Math.round(Math.random()*10)?
I came up with it myself and I’m wondering if I missed a function that can do this already

As far as I know, that’s the easiest way.

Only to abstract it behind your own function, i.e.

function randomInt(min, max) {
// Inclusive of min - Inclusive of max
//return Math.random() * (max - min + 1) + min;
// Inclusive of min - Exclusive of max
return Math.random() * (max - min) + min;
}
1 Like

Perhaps you can try this too:

Solution 1 →

console.log(Math.floor(Math.random() * 10));
// Expected number from 0  to 10
1. The .floor() method convert decimal point to integer.

For example →

console.log(Math.floor(6.95));
// expected output: 6
console.log(Math.floor(-5.05));
// expected output: -6
console.log(Math.floor(7));
// expected output: 7
1. Math.random() will get the random number from 0 to 10.

Solution 2 →

console.log(Math.floor(Math.random() * 10) + 1);
// Expected number from 1 to 10
1. The .floor() method convert decimal point to integer.
2. Math.random() will get the random number from 0 to 10.
3. + 1 returns a random integer from 1 to 10.

Hope it helps.