Which of the following expression yields an integer between 0 and 100, inclusive?
A. (int)(Math.random() * 100 + 1)
B. (int)(Math.random() * 101)
C. (int)(Math.random() * 100)
D. (int)(Math.random() * 100) + 1

Respuesta :

Answer:

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

Explanation:

The function designed as Math.random act as a floating-point of a pseudorandom number with the range between [0,1] which simply means that starting from 0 (inclusive) up to but with the exception of  1 (exclusive), which the design can now be scale to the preferred desired range.

Suppose, we needed 100 inclusive, we have 101 exclusive since we need to scale to 101.