I didn’t bother warning about that b/c the colors[] array already had 3 elements in it.
BtW, here’s an even shorter version w/ just while ()
instead of do // while ()
:
void pick() {
int newIndex;
while ((newIndex = (int) random(colors.length)) == index);
index = newIndex;
}
Nonetheless, a custom shuffle() approach (from my previous posted link) is much more commendable:
@ SafeVarargs final int[] shuffle(final int... arr) {
if (arr == null) return null;
int idx = arr.length;
while (idx > 1) {
final int rnd = (int) random(idx--), tmp = arr[idx];
arr[idx] = arr[rnd];
arr[rnd] = tmp;
}
return arr;
}