Generate Random Whole Numbers within a Range - don't get it

Tell us what’s happening:

Hi guys, I really don’t get what’s happening here and why we’re multiplying a random number by the product of the max minus min and then adding it to the min? Can someone please explain this in basic English?
Also, why won’t this pass the test?
The lowest random number that can be generated by randomRange should be equal to your minimum number, myMin .

The highest random number that can be generated by randomRange should be equal to your maximum number, myMax .

Your 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.

// Only change code below this line.

function randomRange(myMin, myMax) {
  

  return Math.floor(Math.random() * (myMin - myMax + 1)) * myMin; // Change this line

}

// Change these values to test your function
var myRandom = randomRange(5, 15);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range

Hi Amy,

You have to change the return value using your parameters (myMin, myMax).
This will not pass the test because you need if conditions. For instance, a condition specifying that the parameter should not be less or greater than 5 and 15.

your randomRange function multiplies by myMin instead of adds.

take the number 7. multiply by 1.0 you get 7. multiply by 0.0 you get 0.
multiply by 0.5 you get 3.5 and etc anywhere between 0.0 and 1.0 (which Math.random() returns).
so basically that original 7 can ‘scale’ anywhere from 0 to 7 depending on the size of that random 0-1 number.

now do the same thing but shift that 7 on a number line. instead of 0 to 7, shift to the right 3. now it is 3 to 10. There’s still the same measurement between 3 and 10(7). you can still do the same ‘scaling’ of 7, but now you just have to shift that scaled ‘somewhere between’ number to the right 3 proportionally

wtf’s answer is correct. I noticed another little thing: myMin - myMax should be myMax - myMin.

Thanks guys. I think I had gone too long without a break yesterday - silly mistakes! I really appreciate the explanation of what this is doing too. It makes a lot more sense now!

Hi wtf, I understand your answer but I don’t understand why I am doing (max - min + 1) + min and if you can , can you please explain more … Thanks.