Questions about my Color Array

I didn’t bother warning about that b/c the colors[] array already had 3 elements in it. :relieved:

BtW, here’s an even shorter version w/ just while () instead of do // while (): :smirk_cat:

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: :grin:

@ 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;
}
2 Likes