# Am I dumb or is this a bug?

**URL:** <https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648>\
**Category:** Coding Questions\
**Created:** [January 19, 2019, 3:28pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648 "2019-01-19T15:28:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tsskyx](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@Tsskyx](https://discourse.processing.org/u/Tsskyx)\
**Post date:** [January 19, 2019, 3:28pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/1 "2019-01-19T15:28:53Z")

</div>

```auto
void setup() {
}

void draw() {
  fill((frameCount < 60) ? random(255) : color(255, 0, 0));
  rect(0, 0, width, height);
  fill(color(255, 0, 0));
  text("a", width/2, height/2);
}

```

By all means of logic the screen should turn red after 60 frames, but it doesn’t. This is super weird, because the code for the text fill works just fine. So it seems like the structure fill(color(…)) stops working if you include a ternary operand or something? It’s also broken for other similar functions, such as background().

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 19, 2019, 4:42pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/2 "2019-01-19T16:42:06Z")

</div>

I think I understand what is happening. `random(255)` gives a `float`, while `color(255,0,0)` gives an `int`. The thing is that Processing’s `color` type is actually an `int` - for proof, compile and then go to the `source/thing.java` file inside any of the compiled folders, and see that all `color` variable declarations turn into `int`.

Anyways, I think that the ternary operator doesn’t want to give 2 different types of output based on the condition, so it just converts all to `float` - then, `fill()` tries to understand `float(color(255,0,0))`, and interprets it as black.

In other words, use `round(random(255))` instead so the result of that ternary operator is always `int`.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 19, 2019, 4:46pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/3 "2019-01-19T16:46:47Z")

</div>

I problem is not caused by the ternary statement because if you change the first line of draw() to

```auto
fill((frameCount < 60) ? color(random(255)) : color(255, 0, 0, 255));

```

and it works just fine.

Still doesn’t answer why the fill color is not red after 60 frames.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 19, 2019, 4:48pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/4 "2019-01-19T16:48:00Z")

</div>

@Architector_4 what a good answer, didn’t think of that

---

<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:** [January 19, 2019, 9:15pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/5 "2019-01-19T21:15:47Z")

</div>

> [@Architector\_4](#):
>
> use `round(random(255))` instead

For a 0-255 range gray color, `(int) random(256)` also works. 😃

Using `round(random(255))`, each value within the range 1-254 got the same weight to happen. 🤔

However, the values 0 & 255 both got half the change to happen compared to the 1-254 range. 🤪

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 19, 2019, 9:45pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/6 "2019-01-19T21:45:25Z")

</div>

True… At that rate, you could also use `floor(random(256))`…

That is until it rolls exactly `256.000` and you are left wondering what in the world has just happened! :o

So, `min(floor(random(256)),255)` seems to be the best way around for doing this? :v

But that makes it unfair to numbers, as, for example, it can be any number including and between 3 and 4, but it can’t be any number including and between 255 and 256! So, 255 gets whatever-is-floating-point-error-at-that-point less chance _! ! !_

---

<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:** [January 19, 2019, 10:24pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/7 "2019-01-19T22:24:57Z")

</div>

> [@Architector\_4](#):
>
> That is until it rolls exactly `256.000`

Theoretically, that should never happen. For range `0 - 256`, `0` is inclusive and `256` is exclusive. 🧐

> [@Architector\_4](#):
>
> True… At that rate, you could also use `floor(random(256))`

B/c `(int)` is a cast operator, and **floor()** is a function, performance-wise, `(int) random(256)` is faster.

Also, the `(int)` cast operator simply truncates the value, removing any fractional value past the `.`.

While **floor()** skips to the next lowest whole value.  
That is, `floor(1.5)` returns `1`. But `floor(-1.5)` returns `-2`! 🥴

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 19, 2019, 10:44pm UTC](https://discourse.processing.org/t/am-i-dumb-or-is-this-a-bug/7648/8 "2019-01-19T22:44:29Z")

</div>

So I guess the simple answer is don’t let the code cast a color into a float.  
 ![vov](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bdd3c1088d2f93305b688bfd9551f16d8939ccbe.png)
