New projects and new features are going to be added soon. I am working on it, finally have some time. If you have any questions or suggestions, please contact me: workit.js@gmail.com. Currently I am working on BoostBalls project explanation.
This is a free project, and in order for it to continue being free, please consider supporting it.
Random value between min and max value
Function ready to be used
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
Introducing Math.random()
The Math.random() method inMath class returns random value between 0 and 1.
But it doesn't include 1. So actualy it returns value from 0 to 0.999...
const randomValue = Math.random(); // random value from 0 to 1
console.log(randomValue); // 0.09820142543127064
Getting a Random Value within a Range starting from 0
To get a random value within a specific range (for example, from 0 to 10), we multiply the random value by (the range's upper limit + 1) and then round it down usingMath.floor() because of a big amount of decimals.
const randomValue = Math.random(); // random value from 0 to 1
const someValue = 10;
console.log(randomValue * (someValue + 1)); // 7.9242883533580475
console.log(Math.floor(randomValue * (someValue + 1))); // 7
Getting a Random Value with an Offset
Now we want to start from a certain value (e.g., from 10 to 11), you can add that value to the random result. Here's how:
const randomValue = Math.random(); // random value from 0 to 1
const someValue = 10;
console.log(randomValue + someValue); // 10.437221276942248
Getting a Random Value within a Specified Range
We have knowledge how to get a random value within a range starting from 0 and with offset.
Let's try to combine theme:
const randomValue = Math.random(); // random value from 0 to 1
const minValue = 2;
const maxValue = 5;
const minRandomValue = randomValue * (minValue + 1); // random value between 0 and 2
const randomValueBetweenMinAndMax = Math.floor(minRandomValue + maxValue); // 0 + 5 = 5, 2 + 5 = 7!!!
console.log(randomValueBetweenMinAndMax); // 5, 6, 7
Unfortunately we encountered a problem. We get random value between 5and 7. But not what we expected to see from 2 to 5. Why is it happening so?
Let's see what is actually happening in the code.
From minRandomValue we get random value between 0 and 2 and then we add it to maxValue (0, 2) + 5 = (5, 7).
- 0 + 5 = 5 - min output value
- 2 + 5 = 7 - max output value
That is why we weren't getting random value between 2 and 5.
So, we need to somehow get lower min output value
Let's get random value between range values of minValue and maxValue 5 - 2 = 3.
const randomValue = Math.random(); // random value from 0 to 1
const minValue = 2;
const maxValue = 5;
const rangeBetweenMinAndMax = maxValue - minValue; // 5 - 2 = 3
const RandomValueBetweenDifference = Math.floor(Math.random() * (rangeBetweenMinAndMax + 1));
console.log(RandomValueBetweenDifference); // 0, 1, 2, 3
Let's see what we will get if we add minValue to the random value of the range (0, 3) + 2 = (2, 5).
- 0 + 2 = 2 - min output value
- 3 + 2 = 5 - max output value
Congratulations, we finally get this range that we need. Final result is below:
const randomValue = Math.random(); // random value from 0 to 1
const minValue = 2;
const maxValue = 5;
const rangeBetweenMinAndMax = maxValue - minValue; // 5 - 2 = 3
const randomValueBetweenMinAndMax = Math.floor(Math.random() * (rangeBetweenMinAndMax + 1) + minValue);
console.log(randomValueBetweenMinAndMax); // 2, 3, 4, 5
Creating a Reusable Function
Let's wrap this code in function to reuse it in the future:
function randInt(minValue, maxValue){
const randomValue = Math.random();
const rangeBetweenMinAndMax = maxValue - minValue;
const randomValueBetweenMinAndMax = Math.floor(Math.random() * (rangeBetweenMinAndMax + 1) + minValue);
return randomValueBetweenMinAndMax;
}
const minValue = 50;
const maxValue = 100;
console.log(randInt(minValue, maxValue)); // 81
Simplified Function
We understand what is going on inside of randInt(). Let's make it smaller.
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);