You can also use
public static int getRandom(int[] array) {
int rnd = (int)(Math.random()*array.length);
return array[rnd];
}
Math.random()
returns an double
between 0.0
(inclusive) to 1.0
(exclusive)
Multiplying this with array.length
gives you a double
between 0.0
(inclusive) and array.length
(exclusive)
Casting to int
will round down giving you and integer between 0
(inclusive) and array.length-1
(inclusive)