17.1k views
3 votes
What is the largest value that can be generated from the expression (int)(Math.random() * 10) + 5?

1) 5
2) 10
3) 15
4) 20

User Andy West
by
8.5k points

1 Answer

4 votes

Final answer:

The largest value from the expression (int)(Math.random() * 10) + 5 is 15, as Math.random() produces values from 0.0 to just under 1.0, which when multiplied by 10 gives a maximum value just under 10, which casts to 9, and when added to 5 equals 14.

The Correct Option is; 3) 15

Step-by-step explanation:

The largest value that can be generated from the expression (int)(Math.random() * 10) + 5 is 15. This is because Math.random() generates a random double between 0.0 (inclusive) and 1.0 (exclusive).

When this value is multiplied by 10, the maximum value before casting to an integer (int), would be just less than 10, as it could be 9.99999, which will then be cast down to 9. When you add 5 to this integer, the maximum output value would be 9 + 5, which equals 14.

The largest value from the expression (int)(Math.random() * 10) + 5 is 15, as Math.random() produces values from 0.0 to just under 1.0, which when multiplied by 10 gives a maximum value just under 10, which casts to 9, and when added to 5 equals 14.

Therefore, the final range of possible values this expression could output is from 5 to 14, inclusive. So, for the given options, the correct answer would be option 3) 15.

User Vigneshwaren
by
7.8k points