# Having random select from limited choices

**URL:** <https://discourse.processing.org/t/having-random-select-from-limited-choices/12451>\
**Category:** Coding Questions\
**Created:** [July 2, 2019, 2:49pm UTC](https://discourse.processing.org/t/having-random-select-from-limited-choices/12451 "2019-07-02T14:49:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauldefazio](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@pauldefazio](https://discourse.processing.org/u/pauldefazio)\
**Post date:** [July 2, 2019, 2:49pm UTC](https://discourse.processing.org/t/having-random-select-from-limited-choices/12451/1 "2019-07-02T14:49:38Z")

</div>

Hello!

This is a pretty basic question but I haven’t found the answer yet. I’m working with some code from the loop tutorial to fill a screen with gray rectangles, each assigned a random lightness. I’ve posted my code below.

What if I wanted the program to generate the fill from a selection of only 2 or 3 colors… Something like "create rectangle with fill(255,0,0) OR fill(0,255,0)

Here’s the program right now:

```auto
float sqr = 100;

size (900,900);
background(0);
noStroke();
  
for (float y = 0; y < height; y = y + sqr) {
  for (float x = 0; x < width; x = x + sqr) {
  fill(random(255));
  rect(x, y, sqr, sqr);
  }
}

```

Thanks!  
-Paul

---

<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:** [July 2, 2019, 2:53pm UTC](https://discourse.processing.org/t/having-random-select-from-limited-choices/12451/2 "2019-07-02T14:53:50Z")

</div>

Welcome! Please format forum code with the \</\> button, or add three ticks around it. I’ll edit your post to demonstrate.

---

<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:** [July 2, 2019, 4:28pm UTC](https://discourse.processing.org/t/having-random-select-from-limited-choices/12451/3 "2019-07-02T16:28:22Z")

</div>

There are many ways to do what you want.

One simple way is to combine an array (a list of things) with a random integer (a choice about which item in the array to use).

Examples of arrays and random numbers:

- [https://p5js.org/examples/arrays-array.html](https://p5js.org/examples/arrays-array.html)
- [https://p5js.org/reference/#/p5/random](https://p5js.org/reference/#/p5/random)

…and here is a sketch combining them to pick a new random background color when you press any key.

[https://editor.p5js.org/jeremydouglass/sketches/omwYbCOKw](https://editor.p5js.org/jeremydouglass/sketches/omwYbCOKw)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 3, 2019, 5:54am UTC](https://discourse.processing.org/t/having-random-select-from-limited-choices/12451/4 "2019-07-03T05:54:23Z")

</div>

You can load the three colors in an array and then you can retrieve any of these three colors by using the modulos operator (%). Since there are three colors, you can try **value%3** which will return the following values depending on value: 0, 1, 2. You can use these values as the index of your color array.

```auto
int indexColor = value % colorArray.size();
color c = colorTable[indexColor];

```

Kf
