Generate Random Whole Numbers within a Range_help

Tell us what’s happening:

why is there a multiplication symbol as well as addition and subtraction in the formula when its looking for “greater than and less than”.

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.

function randomRange(myMin, myMax) {

  return Math.floor(Math.random() * ; // Change this line

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 12105.100.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.144 Safari/537.36.

Link to the challenge:

So Math.random() does what it says on the box, it spits out a random number. Ish. There are those who would argue it’s not random, yada yada, but for our purposes, it’s random enough. But that number, for all intents and purposes, is kind of useless by itself - it falls between 0 and 1. Anywhere in there. In the infinite gaps and irrational numbers… :wink:

So first, we need to figure the width of the range. Let’s say we wanted to do:

let myRandomNumber = ourRandomRange(50, 100);

So we want a random number with in that range. That is, from 50 to 100 INCLUSIVE. How wide is that range? I mean, how many actual integers would that be? We can find out, using the formula:

let myRangeWidth = ourMax - ourMin+1;
// in our case, myRangeWidth = 100-50+1, or 51 digits wide

So, given the width of the range, if we multiply that by a random number between zero and one, we will end up with a random number between 0 and 51. Right? We might get ANY of the following:


Those are all valid return values of Math.random() * (ourMax-ourMin+1), which we have made Math.random()*51.

I can hear you now: ‘Yes, but 50.29345238 is gonna push us OUTSIDE the range!!’ Ah, grasshopper, but look closely - the next step is Math.floor(50.29345238), which will round it DOWN. To 50. So that whole part, Math.floor(Math.round() * (ourMax - ourMin+1) ), has got us to the point of having an integer between zero and the width of our range. In this case, between zero and fifty.

In order to shift it from zero into our chosen range (50 - 100, inclusive), we simply add 50 to the number we have so far. Or, to go back to the formula, we add ourMin, giving us

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

So, the short answer, read from the INSIDE out: “Take a random number between 0 and 1, and multiply it by the width of our range. Round that down to the nearest integer, then add the minimum value to it. Ship that!”

Hope that helps. In my head it sounded reasonable anyway. :wink:

EDIT: Rereading your question, I realized I hadn’t actually addressed what you asked. The thing is, the wording on that particular question is spoopy. The greater than and less than thing simply means “between the minimum and the maximum values, INCLUDING those values”. So when the range is 50 - 100, then 50 is perfectly acceptable, as is 100.

The confusion comes in because there’s no good way to say “50 to 100, inclusive” in normal conversational lingo. “Between 50 and 100” implies that it’s exclusive, because 50 isn’t between 50 and 100, it’s a border case. So by saying “greater or equal to the minimum value…”, they’re saying “minimum to maximum, INCLUSIVE of those two values.”

1 Like

Thank you for clearing up the wording and logic. I understand it better now.