Q
PRO
2 months ago
Quietcountry asked

The expression Math.random() * 10 + 1 is said to generate a random number from 1.0 to 10.0. But isn’t that incorrect? Shouldn’t the result go from 1.0 up to, but not including, 11.0?

Kelish Rai
Expert
2 months ago
Kelish Rai answered

Hi quiet,

You're absolutely right .

In Java, Math.random() returns a value from 0.0 (inclusive) to 1.0 (exclusive). So:

Math.random() * 10    // gives a value from 0.0 to less than 10.0  
Math.random() * 10 + 1  // gives a value from 1.0 to less than 11.0

So the correct range for Math.random() * 10 + 1 is 1.0 (inclusive) to less than 11.0 (exclusive).

If you have more questions, I am here to help.

Java
This question was asked as part of the Learn Java Basics course.