# Color selected from pallette randomly for ellipses drawn by an array

**URL:** <https://discourse.processing.org/t/color-selected-from-pallette-randomly-for-ellipses-drawn-by-an-array/6204>\
**Category:** Coding Questions\
**Created:** [December 2, 2018, 7:56pm UTC](https://discourse.processing.org/t/color-selected-from-pallette-randomly-for-ellipses-drawn-by-an-array/6204 "2018-12-02T19:56:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![W1nDs0R](https://avatars.discourse-cdn.com/v4/letter/w/439d5e/32.png) [@W1nDs0R](https://discourse.processing.org/u/W1nDs0R)\
**Post date:** [December 2, 2018, 7:56pm UTC](https://discourse.processing.org/t/color-selected-from-pallette-randomly-for-ellipses-drawn-by-an-array/6204/1 "2018-12-02T19:56:13Z")

</div>

Hey there Guys,

I was wondering if you could offer me some insight in to how I can color each ellipse randomly that is drawn by an array.

Currently I have created a second array for colors which it draws from when the sketch is launched but each of the ellipse comes out the same color. How can I make each ellipse select the colour randomly. Here is some of the code.

```auto
void setup() {
  
  index = int(random(colors.length));
  size(500, 500);
  
    for (int i = 0; i < words.length; i++) // X Values
  {
  words[i] = new word(width / 2, height / 2, i*2, index );
  }
  
}

void draw() {
  background(yellow);

  for (int i = 0; i < words.length; i++) // X Values
  {
    words[i].display();
    words[i].floatAimlessly();
    
  }
}

```

// Draw the blob to the canvas  
void display() { // Defines how the words will be displayed

pushMatrix();  
textSize(70);  
noStroke();  
//fill(random(255,200),random(100,255),0);  
fill(colors[index]);  
ellipse(pos.x,y,r,r);  
//text(“Array.”, pos.x, y); // Specify a z-axis value  
popMatrix();

}

![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8778fed5dfa27e0dbab6583e9ffdcd6774959937.png) Here is an image, you can see each ellipse is the same color forming one large shape. I’d like to select from these colors.

```auto
color yellow = color(255, 188, 103); 
color red = color(218, 114, 126); 
color pink = color(172, 108, 130);
color violet = color(104, 92, 121);
color blue = color(62, 78, 89, 100); 

```

```auto

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 2, 2018, 8:15pm UTC](https://discourse.processing.org/t/color-selected-from-pallette-randomly-for-ellipses-drawn-by-an-array/6204/2 "2018-12-02T20:15:30Z")

</div>

Hi,

Start by putting all your colors in an array.  
Then randomly pick an integer in the range of the size of your color array.  
Then use that random number as your index to pick a color in your array.

Small code example:

```auto
color[] myColors;

void setup() {
  size(1280, 1024);
  background(20);
  noStroke();
  
  // Defining the colors
  myColors = new color[5];
  myColors[0] = color(255, 188, 103); 
  myColors[1] = color(218, 114, 126); 
  myColors[2] = color(172, 108, 130);
  myColors[3] = color(104, 92, 121);
  myColors[4] = color(62, 78, 89, 100);
  
  // Creating the ellipse with random colors:
  for (int i = 0; i < 100; i++) {
    int randomColorIndex = (int)random(myColors.length);
    color fillColor = myColors[randomColorIndex];
    fill(fillColor);
    float ellipseSize = random(50, 100);
    ellipse(random(width), random(height), ellipseSize, ellipseSize);
  }
}

```

---

<div class="post-metadata">

**Author:** ![W1nDs0R](https://avatars.discourse-cdn.com/v4/letter/w/439d5e/32.png) [@W1nDs0R](https://discourse.processing.org/u/W1nDs0R)\
**Post date:** [December 2, 2018, 9:36pm UTC](https://discourse.processing.org/t/color-selected-from-pallette-randomly-for-ellipses-drawn-by-an-array/6204/3 "2018-12-02T21:36:20Z")

</div>

Amazing, Thanks so much for you help! I’ll get back to work on it and see how it goes 🙂
