If r is a java.util.Random, you can generate a random number from 0 to n-1 using r.nextInt(n), rather than using (int)(r.nextDouble() * n).
r
java.util.Random
0
n-1
r.nextInt(n)
(int)(r.nextDouble() * n)