# Assign random color to each circle

**URL:** <https://discourse.processing.org/t/assign-random-color-to-each-circle/348>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 24, 2018, 7:17pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348 "2018-05-24T19:17:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcw016](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@jcw016](https://discourse.processing.org/u/jcw016)\
**Post date:** [May 24, 2018, 7:17pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/1 "2018-05-24T19:17:08Z")

</div>

I am trying to complete a school project and this is what I have right now (see below). I want to assign a random color to each circle every time - but right now, I am stuck at where the same color is being assigned to all the circles (a random color each time)… Can anyone help me? I want each circle to have a different shade of brown ☹

```java
Tribble[] tribble = new Tribble[50];
color aColor = color( random(255),random(255),random(255) );

void setup() {
  size(400, 400);
  background(255);
  frameRate(30);
  for (int i = 0; i < tribble.length; i++) {
    tribble[i] = new Tribble(random(0, 400), random(0, 400));
  }
}

void draw() {
  background(255);
  tribble[(int)random(tribble.length)].excited();
  for (int i=0; i<tribble.length; i++) {
    tribble[i].display();
  }
}

class Tribble {
  float xPos, yPos;
  int start;
  float time;
  int offset;
  boolean wakeup;

  Tribble(float x, float y) {
    xPos = x;
    yPos = y;
  }
  //Makes the tribble excited
 void excited() {
time = (frameCount/60) % 7;
if (time >= 4 && time < 6) xPos += random(-1, 1);
}

  //Draws the tribble.
  void display() {
    ellipseMode(CENTER);
    fill(aColor);
    ellipse(xPos, yPos, 50, 50);
  }
}

```

---

<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:** [May 24, 2018, 7:22pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/2 "2018-05-24T19:22:43Z")

</div>

Why don’t you make the variable _aColor_ a field of your class **_Tribble_** as well? 🙄

---

<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:** [May 24, 2018, 7:28pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/3 "2018-05-24T19:28:35Z")

</div>

An online example sketch w/ colored balloons: 🕶  
[http://Studio.ProcessingTogether.com/sp/pad/export/ro.9FqnMDLUZqQCV](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9FqnMDLUZqQCV)

---

<div class="post-metadata">

**Author:** ![jcw016](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@jcw016](https://discourse.processing.org/u/jcw016)\
**Post date:** [May 24, 2018, 8:05pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/4 "2018-05-24T20:05:18Z")

</div>

I’m a little confused because in my code I’m constructing 200 circles at once

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [May 24, 2018, 8:14pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/5 "2018-05-24T20:14:51Z")

</div>

`color aColor` can be an attribute in your `Tribble` class. In the constructor of your class, you can assign it by:

```java
...
  color aColor;
  Tribble(float x, float y) {
    xPos = x;
    yPos = y;
    aColor = color(random(255),random(255),random(255))
  }
...

```

And now, when you’re calling `fill(aColor)` in your display function, you should be getting different colors. You can also experiment with different color spaces like `colorMode(HSB)` and have `acolor=color(random(255),255,255)` so that you specify a certain range of hue you like.

---

<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:** [May 24, 2018, 8:17pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/6 "2018-05-24T20:17:27Z")

</div>

You’ve only got 1 _aColor_ variable:  
`color aColor = color( random(255),random(255),random(255) );`

Instead, turn it into an additional field for your class _ **Tribble** _, like all the other 6 fields already are:

```java
class Tribble {
  float xPos, yPos, time;
  int start, offset;
  boolean wakeup;
  color aColor = (int) random(#000000);
}

```

This way, each instance of _ **Tribble** _ got its own _aColor_ variable. 🙃

---

<div class="post-metadata">

**Author:** ![Repet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/repet/32/189_2.png) [@Repet](https://discourse.processing.org/u/Repet)\
**Post date:** [May 25, 2018, 11:32pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/7 "2018-05-25T23:32:44Z")

</div>

How does the

```auto
(int) random(#000000);

```

work? Isn’t that saying pick a number between 0 and 0? or am I understanding that completely wrong? 🤔

---

<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:** [May 25, 2018, 11:49pm UTC](https://discourse.processing.org/t/assign-random-color-to-each-circle/348/8 "2018-05-25T23:49:20Z")

</div>

Try out: `println(#000000);` on Processing’s IDE (PDE): `-16777216`  
[https://Processing.org/reference/color\_datatype.html](https://Processing.org/reference/color_datatype.html)
