Basic JavaScript - Generate Random Whole Numbers with JavaScript

Tell us what’s happening:
The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9

When I return Math.floor(Math.random()*9) it is not accepted.

The error says " You should have multiplied the result of Math.random by 10 to make it a number that is between zero and nine."

Math.random() will return a value between 0 and 1, so it is possible for it to return .9. If I multiply by 10 It is possible that my result will be 9, which is not between zero and nine, it IS 9, which should fail the test.

Your code so far

function randomWholeNum() {

  // Only change code below this line

 
  return Math.floor(Math.random()*10);
}

Your browser information:

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

Challenge: Basic JavaScript - Generate Random Whole Numbers with JavaScript

Link to the challenge:

The Math.random() static method returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1

Returning 1 is not possible

Thanks Jeremy, that point was made clear in the lesson, and actually highlights the issue I’m raising.

It can return .9. Multiply by 10 and you get 9.

When something is “between” 2 numbers, that means it is not equal to those numbers. It is less than the upper range, and more than the lower range.

9 is not between 0 and 9. It is equal to 9

In computer science, “between” is typically inclusive, meaning it does include those numbers.

So, if you were asked to write a function to calculate the amount of space BETWEEN two parked vehicles you would include the space that those vehicles occupy?

That’s unrelated to the language being used here.

In this context, its conventional to include both 0 and 9. The instructions clearly tell you to do so.

I don’t control what the linguistic conventions are. I can just tell you what they are.

I disagree.

It’s confusing.

Perhaps your text reads differently than mine.

I see

  1. Use Math.random() to generate a random decimal.
  2. Multiply that random decimal by 20.
  3. Use another function, Math.floor() to round the number down to its nearest whole number.

Remember that Math.random() can never quite return a 1 and, because we’re rounding down, it’s impossible to actually get 20. This technique will give us a whole number between 0 and 19.

This states that “a whole number between 0 and 19” includes both 0 and 19. :person_shrugging:

I would guess that 99.999999% of the people taking this course are not computer scientists.

I am not a computer scientist, so to me between means the area separating 2 things, which by default means exclusive. If the term between has a different meaning in javascript that should be clearly explained before it is used.

I am not whining or complaining. I would like to see this course get better. I have struggled at times because of the language being used in the lessons and challenges.

I can’t change the conventional use of that word in computer science for you, unfortunately.

Like I said, maybe your text doesn’t have the full explanation that shows what they mean? This isn’t some special Javascript only usage of the term.

The usage of “between” in MDN can be even a bit more complicated unfortunately, as they are inclusive of the lower and exclusive of the upper in the first two links here:

The last one uses language similar to the next challenge:

I wouldn’t necessarily be opposed to clarifying:

This technique will give us a whole number between 0 and 19, inclusive.

I agree.

The word “between” even when clarifying it using inclusive/exclusive is easy to confuse. I feel like “range” or using from/to or starting/ending would be easier to understand.

This technique will give us a whole number ranging from 0 to 19

This technique will give us a whole number starting from 0 and ending with 19

They might still be a bit awkward.

1 Like

The “, inclusive” is the clarifier I’ve most often seen but it can be confusing for people unfamiliar with the word.

An example is clearest. There is a code example here.,

I’m not sure the code example really helps here. The word “between” is a questionable choice in my opinion.

The specs use range as well, even if it is in the usual spec gobbledygook speak.

21.3.2.27 Math.random ( )

This function returns a Number value with positive sign, greater than or equal to +0𝔽 but strictly less than 1𝔽, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-defined algorithm or strategy.

We can use the whole phrase “a whole number bigger than or equal to 0 and less than or equal to 19.” if we are concerned that “between” isn’t being understood by learners generally.

Since whenever it was last updated, this Challenge has received nearly zero questions while the next Challenge has received many more questions, so I don’t know that this change will change much?



We can generate random decimal numbers with Math.random(), but sometimes we need to generate random whole numbers. The following process will give us a random whole number less than 20:

  1. Use Math.random() to generate a random decimal number.
  2. Multiply that random decimal number by 20.
  3. Use Math.floor() to round this number down to the nearest whole number.

Remember that Math.random() can never quite return a 1, so it’s impossible to actually get 20 since we are rounding down with Math.floor(). This process will give us a whole number bigger than or equal to 0 and less than or equal to 19.

Putting everything together, this is what our code looks like:

Math.floor(Math.random() * 20);

We are calling Math.random(), multiplying the result by 20, then passing the value to Math.floor() function to round the value down to the nearest whole number.


Use this technique to generate and return a random whole number in the range from 0 to 9.

I would keep the same wording in the challenge text as in the instruction.


Clarifying language is never a bad idea.

We can generate random decimal numbers with Math.random(), but sometimes we need to generate random whole numbers. The following process will give us a random whole number less than 20:

  1. Use Math.random() to generate a random decimal number.
  2. Multiply that random decimal number by 20.
  3. Use Math.floor() to round this number down to the nearest whole number.

Remember that Math.random() can never quite return a 1, so it’s impossible to actually get 20 since we are rounding down with Math.floor(). This process will give us a whole number in the range from 0 to 19.

Putting everything together, this is what our code looks like:

Math.floor(Math.random() * 20);

We are calling Math.random(), multiplying the result by 20, then passing the value to Math.floor() function to round the value down to the nearest whole number.


Use this technique to generate and return a random whole number in the range from 0 to 9.

Look pretty good to me. Are you going to open an issue?

I think the MDN page does a great job of clearly specifying inclusive and exclusive in their explanations.

To boot, they also provide an example of a function that is inclusive at the minimum and maximum:

Getting a random integer between two values, inclusive

While the getRandomInt() function above is inclusive at the minimum, it’s exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive() function below accomplishes that.

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min); // The maximum is inclusive and the minimum is inclusive
}

If you’re opening an issue, maybe include the above in the lesson?

That example is the next lesson. It doesn’t go in this lesson.

We should not be including all possible related discussion in the text - the more text included, the less people read.