Basic JavaScript - Generate Random Whole Numbers within a Range

Can someone please guide me how is this code working??

Math.floor(Math.random() * (max - min + 1)) + min
function randomRange(myMin, myMax) {
  // Only change code below this line
  return 0;
  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Basic JavaScript - Generate Random Whole Numbers within a Range

Link to the challenge:

Could you tell me how many numbers are in between 5 and 89? Can you walk me through how to find how many numbers are between any two numbers with a formula?

Between 5 and 89:
((k-n)+1) = (89-5) + 1
84+1=85

But what is the purpose of adding last min number?

With that, this

Math.floor(Math.random() * (max - min + 1)) + min

becomes

Math.floor(Math.random() * (NUMBER OF POSSIBLE VALUES)) + min

There are two pieces to this

Math.floor(Math.random() * (NUMBER OF POSSIBLE VALUES))

This piece makes a random number between 0 and NUMBER OF POSSIBLE VALUES and

 + min

this part adds min to it so the smallest possible value you will get is min

2 Likes

Completely got it . Thank you!

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