# Randomly pick one variable

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?

2 Likes

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

``````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;
}
``````
3 Likes

A shorter 1 using the ternary conditional operator `?:`:
Processing.org/reference/conditional.html

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

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

println(pick);
exit();
``````
2 Likes

Thanks, they worked, I do have another question what MIN_INT mean?

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

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?

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

Much probably youâ€™ve got something else in mind when MIN_INT is the result.

I am thinking that pick can be = MIN_INT, is that wrong?

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

1 Like

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

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:

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

1 Like

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

Can you please explain it for me?

1 Like

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

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

1 Like

Which one did you pick?

`:)`

1 Like

@glv I decide to pick the third one :

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

but they are all good.

3 Likes