# Questions about my Color Array

**URL:** <https://discourse.processing.org/t/questions-about-my-color-array/3110>\
**Category:** Coding Questions\
**Created:** [August 30, 2018, 2:17pm UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110 "2018-08-30T14:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@Benjamin](https://discourse.processing.org/u/Benjamin)\
**Post date:** [August 30, 2018, 2:17pm UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/1 "2018-08-30T14:17:18Z")

</div>

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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 31, 2018, 7:11pm UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/2 "2018-08-31T19:11:38Z")

</div>

```auto
void pick() {
  int newIndex;

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

  index = newIndex;
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 3, 2018, 3:10am UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/3 "2018-09-03T03:10:10Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 3, 2018, 3:18am UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/4 "2018-09-03T03:18:10Z")

</div>

- [Forum.Processing.org/two/discussion/20529/retrieving-elements-from-a-list-randomly](http://Forum.Processing.org/two/discussion/20529/retrieving-elements-from-a-list-randomly)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 3, 2018, 3:37am UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/5 "2018-09-03T03:37:54Z")

</div>

> [@jeremydouglass](#):
>
> … because the while loop will run forever and never exit…

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 ()`: 😼

```auto
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: 😁

```java
@ 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;
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 3, 2018, 4:30am UTC](https://discourse.processing.org/t/questions-about-my-color-array/3110/6 "2018-09-03T04:30:00Z")

</div>

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.
