JavaScript JavaScript Numbers The Math Object Random Number Challenge – Two Numbers Solution

Varun Jain
Varun Jain
Courses Plus Student 1,892 Points

Really not understand the math euqation of lownumber and highnumber. which is (highnumber - lownumber + 1);

I am trying to figure out this. Why we put this math formula in code.

1 Answer

Tobias Kloy
Tobias Kloy
3,653 Points

The Math.random() function creates a number between 0 and 1 while 0 is included but 1 not.

Let's say:

var lowNumber = 2;
var highNumber = 8;
var randomNumber = 0.17237478506267383;

If you now take randomNumber * (highNumber - lowNumber) you would receive a number below 1 and with Match.floor() it would turn to 0. That is why we add 1. The lower and higher numbers are just for fun, we could also use a single number instead.