Questions about my Color Array

Hi, I’m new to programming and have a few questions about a small piece of code that I am currently writing in Processing 3. So at the moment I have an array with four different colors in it that represent the four different seasons and when I left click, it randomly picks a color from the list. Currently when I click, there’s a chance that the new color will be a duplicate of what I already have. What I’m looking for is something that removes the current color from the array temporarily and adds it back to the array once changed to a different color.

I also need help in fading the colors from one to the other as you click. Any and all help is much appreciated.

color[] colors = { 
  color(93,177,227), 
  color(166,187,204), 
  color(221,186,138), 
  color(76,112,187)

};
int index;
 
void setup() {
  size(500, 500);
  pick();
}
 
void pick() {
  index = int(random(colors.length));
}
 
void draw() {
  background(colors[index]);
}
 
void mousePressed() {
  pick();
}
void pick() {
  int newIndex;

  do newIndex = (int) random(colors.length);
  while (newIndex == index);

  index = newIndex;
}

You described one algorithm for doing what you want to do:

  • Remove the current index
  • Choose new index randomly from the remaining
  • Re-add old index

But there are others. @GoToLoop gives one alternate algorithm:

  • Randomly choose a new index
  • Check if new equals old. If it does, re-choose and check again.

…and there are even more approaches.

My personal favorite is to spin the index randomly by between 1 and to n-1 positions. So, if you imagine your choices as a 4-quarter pie, you are going to randomly spin it by between 1-3 positions. That way you will never repeat as long as you have 2 or more items.

void setup(){
  int[] vals = {0, 10, 20, 30};
  int index = 0;
  // print 9 non-repeating values
  for(int i = 0; i < 9; i++){
    index = spin(index, vals.length);
    println(vals[index]);
  }
}

/**
 * takes an index and the length of an array,
 * returns a new index
 */
int spin(int index, int len){
    int amt = 1 + (int)random(len-1);
    return (index + amt)%len;
}

An interesting thing to note about this last algorithm is that it functions correctly with an array of any length – including in particular an array of 1 item (try it). The previous two algorithm both break at length 1 – the first because the list is now empty (oops!) and @GoToLoop’s because the while loop will run forever and never exit (1 = 1 = 1 = 1 = 1 …).

So if you are going to be adding and remove items from a list and want to be able to pull a random item from that list – but the list could go down to 1 item – then you will have to either special-case length=1 or use this last general approach with modulo offsets (or one like it).

2 Likes

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

Note that a shuffled index set is like a deck of cards:

2, 0, 1, 3
1, 3, 2, 0

…while a non-repeating index simple guarantees each new number is a change, but is not globally distributed:

0, 1, 0, 2
2, 3, 2, 3

The question is which you want your click behavior to do.