Why is a whole equation needed when we can just use Math.random?

Can anyone explain it as simple as possible why is here this whole equation needed when we can simply generate random integer number by Math.floor(Math.random()) * 10?

I still can not figure it out.

My code so far


// Example
function ourRandomRange(ourMin, ourMax) {

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

ourRandomRange(1, 9);

// Only change code below this line.


function randomRange(myMin, myMax) {
 var randomNumber = " ";var myRandom = randomRange(5, 15);
  function randomizer() { return Math.floor(Math.round()) * 10; }
    if (randomNumber >= myMin && randomNumber <= myMax) {
  return randomNumber; // Change this line
} else { randomizer(); }
}

// Change these values to test your function

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.2988.0 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/generate-random-whole-numbers-within-a-range

The challenge is: https://www.freecodecamp.org/challenges/generate-random-whole-numbers-within-a-range

Generate Random Whole Numbers Within A Range

What is all this for?? You just have to change our in the example to my in yours, it’s just trying to get you to write out and understand what ourRandomRange is doing.

Because it’s not asking for any random integer, it’s asking you for a random integer between min and max. Math.floor(Math.random()) * 10 will just return 0 every single time - random gives you a number between 0 and 1 (not including 1). floor rounds that down to the nearest integer, which is always 0. 0 * 10 is 0.

Math.floor(Math.random() * 10), which is what I think you meant, just gives an integer that is 0,1,2,3,4,5,6,7,8 or 9, that’s it.

1 Like

Hi Dan,

thank you a lot for the immediate response! I was trying to figure out the solution of the exercise called “Generate Random Whole Numbers Within A Range”. Here is the link to it.

The one thing I do not understand at all, is why we are instructed by FreeCodeCamp to write the following equation: Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;

What purpose is the substraction of ourMin from (ourMax - ourMin + 1) serving and why do we need this as a whole: Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin

Instead of just Math.floor(Math.random()) * 10 to get a random integer?

This doesn’t produce a random integer, it produces 0 every time.

Because it’s not asking for any random integer, it’s asking you for a random integer between min and max. Math.floor(Math.random()) * 10 will just return 0 every single time - random gives you a number between 0 and 1 (not including 1). floor rounds that down to the nearest integer, which is always 0. 0 * 10 is 0.


Math.floor(Math.random() * 10), which is what I think you meant, just gives an integer that is 0,1,2,3,4,5,6,7,8 or 9, that’s it.

The numbers 0-9 aren’t ‘any random integer’, they’re just the integers 0-9.

Say you have a min of 5 and a max of 15. You want a number that is 5,6,7,8,9,10,11,12,13,14 or 15.

The + 1 in max - min + 1 is because random is non-inclusive. It returns a number between 0 and 1, never 0 and never 1.

So for example, if you want the numbers between 0 and 10, including 0 and 10, you could do Math.floor(Math.random() * 11). The + 1 ensures you can get 10 back sometimes: it will never actually be 11 so floor always rounds down to 10 if the unrounded value is something like 10.93412354124.

If you wanted the numbers between 5 and 10, you could do Math.floor(Math.random() * 6), which gives you the numbers between 0 and 5, including 0 and 5. Then if you + 5 to that, you’re going to get the numbers between 5 and 10 rather than 0 and 5

The + min makes sure the lowest possible value is the minimum value.

So you multiply the result of Math.random by the length of the range you want + 1, round down the result to an integer, then add the minimum value.

Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin
Math.floor(Math.random() * (15 - 5 + 1)) + 5
Math.floor(Math.random() * 11) + 5

So say math.random returns 0.5543656546435645.

Math.floor(0.5543656546435645 * 11) + 5
Math.floor(6.098022201079209) + 5
6 + 5
11

Or say 0.999999999999

Math.floor(0.999999999999 * 11) + 5
Math.floor(10.999999999989) + 5
10 + 5
15

Or say 0.0000000000001

Math.floor(0.0000000000001 * 11) + 5
Math.floor(1.1e-12) + 5
0 + 5
5
1 Like

Thank you a lot for your help! Now I understand it much better, I just noticed I have misread one part of the instructions.

Great answers, guys!

1 Like