Get a number randomly from a range of numbers in JS
In this example, we have a task of writing a function that will take two inputs, minimum and maximum numbers, and randomly get a number from this range.
By: Ajdin Imsirovic 07 January 2021
Let’s declare our function:
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(minNum, maxNum);
}
For starters, we’ve named our function, and we’ve specified the expected parameters. If we call the randomNumFromRange
function now, we’d get the passed-in arguments’ values logged out to the console.
Now we’ll use Math.random()
.
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(Math.random());
}
This will give us any range from inclusive 0
to exclusive 1
. But how do we do it for a range of, for example, 30
to 60
?
To solve this issue, we need to think about our maximum number in the range, which is 60
. How do we get it?
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(Math.floor(Math.random() * 60) + 1);
}
With this, we’ll get any number between 1
and 60
. To prove it, let’s look at the lowest possible result of Math.random()
:
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(Math.floor(0 * 60) + 1);
}
Obviously, the result of the above will always be 1
. How about using the largest possible result of Math.random()
?
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(Math.floor(0.99 * 60) + 1);
}
The result of Math.floor(0.99 * 60)
is 59
, and with a + 1
, we get 60
.
So, we’re indeed never going to get the result over 60
. Now let’s look at our code so far and think about how to get the result of not less than 30
.
To do that, let’s begin with replacing the Math.floor()
with Math.ceil()
, and a + 1
with a + 0
.
1
2
3
function randomNumFromRange (minNum, maxNum) {
console.log(Math.ceil(Math.random() * 60) + 0);
}
Now we need to get a range, using:
This will give us a range of 30 numbers, starting anywhere from 0
to 30
.
To move the range of 0...30
to 30...60
, we need to move the “slider” by 30
numbers up; this is the exact value of our minNum
.
This results in the following code:
1
2
3
4
5
6
7
function randomNumFromRange (minNum, maxNum) {
console.log(
Math.ceil(
(Math.random() * 60) - (Math.random() * 30) + 30
)
);
}
Now we can remove magic numbers and use parameters instead:
1
2
3
4
5
6
7
function randomNumFromRange (minNum, maxNum) {
console.log(
Math.ceil(
(Math.random() * maxNum) - (Math.random() * minNum) + minNum
)
);
}
You can test it out by calling it like, for example, this:
It’s good to test it this way because you can expect to get only 2 numbers, either a 31
or a 32
.
We can further simplify our code like this:
1
2
3
4
5
6
7
function randomNumFromRange (minNum, maxNum) {
console.log(
Math.ceil(
Math.random() * (maxNum - minNum) + minNum
)
);
}
To reiterate, in the above code, maxNum - minNum
sets up the range, and + minNum
moves it’s starting point from zero to the value of minNum
.
Finally, we can re-write this code as an ES6 function:
1
2
3
4
5
const randomNumFromRange = (minNum, maxNum) => {
console.log(Math.ceil(
Math.random() * (maxNum - minNum) + minNum
));
}
Note:
This exercise comes from Book 3
of my book series on JS, available on Leanpub.com.