Questions about my Color Array

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).

4 Likes