# Challenge: Generate random whole number

Challenge: Generate random whole number
0

#1

Hi. I am having some difficulty understanding this challenge.
In the example. It is my understanding that ourMax is equal to 9 and ourMin is equal to 1.
// Example

``````function ourRandomRange(ourMin, ourMax) {

return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
}

ourRandomRange(1, 9);
``````

Now in the return statement the section to the right of the multipication symbol reads like this to me. (9-1+1) + 1, which to me equates to 10.
Math.floor rounds down to nearest whole integer correct?
Math.random() gives a float between 0 and .9999 correct?
So does Math.floor round down to zero?
If so this should result in 0 * 10 which equates to zero.
But the console logs 1.
I really don’t get it.
Can someone please clear this up for me?

#2

You’re absolutely right that `Math.floor` will give us 0, and to the right of that we add `ourMin` one more time to make sure we’re at the minimum.

#3

Math.floor rounds down to nearest whole integer and + 1 is added after the math floor so it will always be at least 1 in this example (the ourMin value).

Say math random generated 0.9999 the formula would give…

Math.floor(0.9999 * (9 - 1 + 1)) + 1;
Math.floor(8.9991) + 1;
// Math.floor rounds down to nearest whole integer so…
8 + 1 = 9

Say math random generated 0.1111 the formula would give…

Math.floor(0.1111 * (9 - 1 + 1)) + 1;
Math.floor(0.9999) + 1;
// Math.floor rounds down to nearest whole integer so…
0 + 1 = 1

I think thats right anyway! lol

#4

Math.floor() round to its integer… like we know Math.random() gives value between 0-0.999999…
what Math.floor() does is ,it round to integer , i.e to zero…
Math.floor(Math.random()) will always give value of zero.
But if we write like
Math.floor(Math.random()*10) then then possible output will be between 0-9;
because maximum value in the case of (Math.random()*10= 9.9999;
and thus Math.floor() will bring it to 9…
i.e what we multiply with Math.random() , Math.floor() will give value 0-(N0.-1) value.

here is image… look at this. your confusion will clear

#5

Thank you all for your responses. This has cleared things up for me.