# How do you use random without the numbers in between

**URL:** <https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144>\
**Category:** Beginners\
**Created:** [February 5, 2019, 10:16pm UTC](https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144 "2019-02-05T22:16:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Birnfeld](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@Birnfeld](https://discourse.processing.org/u/Birnfeld)\
**Post date:** [February 5, 2019, 10:16pm UTC](https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144/1 "2019-02-05T22:16:57Z")

</div>

im doing a simple code and i want the variable to be -5 or +5 but i dont understand how you can do that without the numbers in between, for exemple sometimes i get -3,4,2,-1,2 ect…

---

<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:** [February 5, 2019, 10:30pm UTC](https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144/2 "2019-02-05T22:30:11Z")

</div>

`const rnd = random(1) > .5? 5 : -5;`

> **[Conditional (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)**
>
> The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

---

<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:** [February 7, 2019, 10:08pm UTC](https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144/3 "2019-02-07T22:08:54Z")

</div>

1. You want two values
2. First you need to generate a random heads/tails to pick between those two values – like this: `random(1) > .5`
3. …then you need to convert that result into your two values.

This is what the ternary that @GoToLoop shared does – but you can also do this without the ternary operator.

1. generate a random 0 or 1: `(int)random(1)`
2. map that to -5, 5: `map((int)random(1), -5, 5)`;
3. now if you give the map “0” you get -5, and if you give it “1” you get 5.

Or, you could do it like an equation:

`int x = ((int)random(1) * 10 - 5`

So, we get a 0 or 1, then it becomes 0 or 10, then it becomes -5 or 5.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 8, 2019, 5:06am UTC](https://discourse.processing.org/t/how-do-you-use-random-without-the-numbers-in-between/8144/4 "2019-02-08T05:06:06Z")

</div>

Personally I prefer to use `<` (less than) to `>` (greater than) because it helps when you want one option to be more likely than the other option.

Some examples:

```auto
const rnd = random(1) < .3 ? 5 : -5; // 30% of the times you get 5, 70% you get -5
const rnd = random(1) < .5 ? 5 : -5; // both options same probability
const rnd = random(1) < .7 ? 5 : -5; // 70% of the times you get 5, 30% you get -5

```

or maybe more clear:

```auto
const rnd = random(100) < 30 ? 5 : -5; // 30% of the times you get 5, 70% you get -5
const rnd = random(100) < 50 ? 5 : -5; // both options same probability
const rnd = random(100) < 70 ? 5 : -5; // 70% of the times you get 5, 30% you get -5

```

If you use `>` the percentages are inverted which may be harder to understand:

```auto
const rnd = random(100) > 30 ? 5 : -5; // 70% of the times you get 5, 30% you get -5

```

Update: I never thought about it like this, but maybe `<` and `>` can be equally easy to understand if you think that they point towards the item with the given probability. So `> 30` means that the right item has 30% chances, and `< 30` means that the left one has 30% chances.
