Basic JavaScript: Generate random whole numbers within a range

Tell us what’s happening:
I’ve read through this thread, but, probably due to my lack of mathematics aptitude, I don’t really grasp the concept. Is this a common problem, having to generate random numbers? Is this method for generating a random number common, or specific to JavaScript? How detrimental is it not to understand this concept?

  **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
}

console.log(randomRange(5, 10)); // ignore!
  **Your browser information:**

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

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

For smaller scale projects, needing a (pesudo)random integer generator can be somewhat common, but you really only need to write the function once. For larger projects, you often call into a library or have already written a function to do this.

1 Like

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