42.3k views
3 votes
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

User Vinita
by
5.0k points

1 Answer

6 votes

Answer:

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

Step-by-step 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.

User The Real Bill
by
5.3k points