Mistake in formula?


Congrats for your nice training system. With all due respect, I think that the correct formula should be return Math.floor(Math.random()(myMax-myMin)) + myMin;*, i.e. the “+1” leads to a value outside the range between myMin and myMax.

Kind regards.


  **Your code so far**

function randomRange(myMin, myMax) {
// Only change code below this line
return Math.floor(Math.random()*(myMax-myMin+1)) + myMin;
// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

Does it though?
Please refer to 2 lessons before:

JavaScript has a Math.random() function that generates a random decimal number between 0 (inclusive) and 1 (exclusive). Thus Math.random() can return a 0 but never return a 1 .

Math.random() NEVER returns 1, so if we Math.floor() that, we will never reach the highest number. Hence we increase it by one. Because we multiply the random number, we can still get 0. But we can never get MyMax, unless we increase by “something” - doesn’t exactly have to be 1, but any number larger than 0 and smaller than 2.

Dear Jagaya

Thanks for your feedback. Thanks I missed the impact of the second Math.floor function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.