# 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:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 26, 2020, 4:58am UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/1 "2020-05-26T04:58:06Z")

</div>

Is there a way to randomly pick between two values. For example, randomly pick `a` or `b`, not anything in between, and if possible, do this when a = -1 and b = 1?

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 26, 2020, 7:24am UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/2 "2020-05-26T07:24:05Z")

</div>

Of course it is. I’m not sure I understood what you meant. There are shorter ways to do this, but less readable and understandable

```auto
int ran;
if(a == -1 && b == 1) { //check if a = -1 and b = 1
  if(random(2) < 1) { //Generate random number between 0 and 2. If < 1 set ran=a,
    ran = a;
  } else { //else random(2) is >= 1. set ran=b
    ran = b;
}

```

---

<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 26, 2020, 8:34am UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/3 "2020-05-26T08:34:55Z")

</div>

> [@SomeOne](#):
>
> There are shorter ways to do this,

A shorter 1 using the ternary conditional operator `?:`: 😉

> **[?: (conditional) / Reference](https://processing.org/reference/conditional.html)**
>
> A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test …

```auto
int a = -1, b = 1, pick = MIN_INT;
if (a == -1 & b == 1) pick = random(2) < 1? a : b;

println(pick);
exit();

```

> [@SomeOne](#):
>
> , but less readable and understandable.

```auto
int a = -1, b = 1;
int pick = a == -1 & b == 1? random(2) < 1? a : b : MIN_INT;

println(pick);
exit();

```

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 26, 2020, 3:47pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/4 "2020-05-26T15:47:51Z")

</div>

Thanks, they worked, I do have another question what MIN\_INT mean?

---

<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 26, 2020, 4:11pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/5 "2020-05-26T16:11:39Z")

</div>

[Processing.GitHub.io/processing-javadocs/core/processing/core/PConstants.html#MIN\_INT](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PConstants.html#MIN_INT)

Some arbitrary value to flag it failed to pick 1 of the 2 **random()** values. 🤓

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 26, 2020, 4:15pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/6 "2020-05-26T16:15:37Z")

</div>

Got it so if a, and b are not = to 1, and -1, MIN\_INT would be true, but wouldn’t it be printed in the console too?

---

<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 26, 2020, 4:44pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/7 "2020-05-26T16:44:47Z")

</div>

**print()** to console is merely to log what happened.

Much probably you’ve got something else in mind when MIN\_INT is the result.

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 26, 2020, 4:52pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/8 "2020-05-26T16:52:28Z")

</div>

I am thinking that pick can be = MIN\_INT, is that 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 26, 2020, 5:05pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/9 "2020-05-26T17:05:58Z")

</div>

Yes, if the 1st `a == -1 & b == 1` fails.

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 26, 2020, 5:29pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/10 "2020-05-26T17:29:41Z")

</div>

If it fails then the print would print MIN\_INT because it is the only thing that is true now, right?

---

<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.

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 27, 2020, 11:21pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/12 "2020-05-27T23:21:53Z")

</div>

I am trying to the heads or tails method. I understand the first method and the last one, but I don’t get this:

> [@jeremydouglass](#):
>
> …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

Can you please explain it for me?

---

<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 28, 2020, 10:14pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/13 "2020-05-28T22:14:37Z")

</div>

> [@jeremydouglass](#):
>
> (int)random(2)

Try wrapping each one in println() to see what it does. Maybe wrapping that in a for loop to see it multiple times.

```auto
for (int i=0; i<10; i++) {
  println(
    (int)random(2)
  );
}

```

`random(x)` returns a random float in the range 0-x. `(int)` casts the float to an integer.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 28, 2020, 11:06pm UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/14 "2020-05-28T23:06:25Z")

</div>

Which one did you pick?

`:)`

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 29, 2020, 12:04am UTC](https://discourse.processing.org/t/randomly-pick-one-variable/21304/15 "2020-05-29T00:04:16Z")

</div>

@glv I decide to pick the third one :

```auto
int[] vars = new int[] { -1, 1 };
println(vars[(int)random(2)] );

```

but they are all good.
