# Randomly pick one variable

**URL:** <https://discourse.processing.org/t/randomly-pick-one-variable/21304>\
**Category:** Beginners\
**Created:** [May 26, 2020, 4:58am UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304 "2020-05-26T04:58:05Z")\
**Posts on this page:** 1\
**Showing post:** 11

<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:** [May 27, 2020, 8:59pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/11 "2020-05-27T20:59:56Z")

</div>

> [@Adi](#):
>
> if possible, do this when a = -1 and b = 1?

This thread is assuming that you meant this as a state condition. But I suspect you just mean that these are the ranges you want. Does the value of `a` ever change?

Or do you just mean that you want to flip a coin, heads = -1, tails = 1? Possibly with arbitrary values on the coin?

Consider:

`random(1)` : 0.41… 0.74… 0.22… 0.52… 0.10… 0.87…  
`round(random(1))` : 0 1 0 0 1 1 1 0 1 0 1 1 0 0 …  
…or `(int)random(2)` : 0 1 0 0 1 1 1 0 1 0 1 1 0 0 …  
`2 * round(random(1))` : 0 2 0 0 2 2 2 0 2 0 2 2 0 0 …  
`2 * round(random(1)) - 1` : -1 1 -1 -1 1 1 1 -1 1 -1 1 1 -1 -1 …

Now, this is just computing a random 1 or -1 – it isn’t picking a variable at random. However, there are many ways to connect a choice between two and a random number. For example:

```auto
int[] vars = new int[] { -1, 1 };

```

Now we have a vars[0] and a vars[1] holding our values. Let’s pick one at random.

`println( vars[(int)random(2)] )`

This will look up either vars[0] (= -1) or vars[1] (= 1). Now you could put any values in there, like `{ -13, 4 }` and `vars[(int)random(2)]` would always return one of those two values at random.

---

_[View the full topic](https://discourse.processing.org/t/randomly-pick-one-variable/21304)._
