225k views
2 votes
Which of the following expressions yields an integer between 0 and 100, inclusive? Question 5 options: (int)(Math.random() * 100 + 1) (int)(Math.random() * 101) (int)(Math.random() * 100) (int)(Math.random() * 100) + 1

User Kamilos
by
3.5k points

1 Answer

3 votes

Answer:

(b) (int)(Math.random() * 101)

Step-by-step explanation:

Given


min = 0 --- minimum


max =100 --- maximum

Required

Java expression to generate random integer between the above interval

The syntax to do this is:

(int)(Math.random((max - min) + 1) + min)

Substitute the values of max and min

(int)(Math.random((100 - 0) + 1) + 0)

Simplify the expression

(int)(Math.random(100 + 1) + 0)

(int)(Math.random(101) + 0)

(int)(Math.random(101))

Hence, the right option is:

(b) (int)(Math.random() * 101)

User Alapan Das
by
3.4k points