Math.random() returns a double value greater than or equal to 0.0 and strictly less than 1.0. Multiplying this by 3 yields a value in the range [0.0, 3.0), and casting this result to an integer truncates the decimal portion, resulting in the integers 0, 1, or 2.