# Random variable

**URL:** <https://discourse.processing.org/t/random-variable/4797>\
**Category:** Beginners\
**Created:** [October 24, 2018, 9:19am UTC](https://discourse.processing.org/t/random-variable/4797 "2018-10-24T09:19:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)\
**Post date:** [October 24, 2018, 9:19am UTC](https://discourse.processing.org/t/random-variable/4797/1 "2018-10-24T09:19:02Z")

</div>

Would this produce a 1/10 chance that action A is played?

```auto
float ran = random(10);
if (ran == 2)
{
  //action A
}
else if (ran != 2)
{
  //action 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:** [October 24, 2018, 9:29am UTC](https://discourse.processing.org/t/random-variable/4797/2 "2018-10-24T09:29:06Z")

</div>

**random()** returns a `float` datatype value: [Processing.org/reference/random\_.html](http://Processing.org/reference/random_.html)

You need an `int` instead: 🤓

```java
final int choice = (int) random(10); // range 0 to 9

if (choice == 2) {
}
else {
}

```

---

<div class="post-metadata">

**Author:** ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)\
**Post date:** [October 24, 2018, 11:14am UTC](https://discourse.processing.org/t/random-variable/4797/3 "2018-10-24T11:14:11Z")

</div>

@GoToLoop

Would this also work?

```auto
int ran = (int)random(10);

```

Then I would be able to keep my previous code

```auto
if (ran == 2)
{
  //action A
}
else if (ran != 2)
{
  //action B
}

```

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [October 24, 2018, 11:31am UTC](https://discourse.processing.org/t/random-variable/4797/4 "2018-10-24T11:31:51Z")

</div>

Yes, it would. You don’t need the `if(ran != 2)` bit though–just the `else` will do. This is because the else bit only runs if the if stuff before hasn’t run.

---

<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:** [October 24, 2018, 11:32am UTC](https://discourse.processing.org/t/random-variable/4797/5 "2018-10-24T11:32:57Z")

</div>

> [@Lore](#):
>
> Would this also work?

Yes, it would alright! 😉

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 24, 2018, 11:35am UTC](https://discourse.processing.org/t/random-variable/4797/6 "2018-10-24T11:35:08Z")

</div>

That Would work too, since it means the Same. But the second if (ran != 2) is obsolete. If it is 2, then the if (ran == 2) is true, Else the other one is. What you are doing is just adding a redundant condition. You can do it, But it’s better to not, performance wise. (Doesn‘t matter much now, But it stacks if you do it this way some Million Times.

---

<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:** [October 30, 2018, 12:17am UTC](https://discourse.processing.org/t/random-variable/4797/7 "2018-10-30T00:17:17Z")

</div>

> [@Lore](#):
>
> Would this produce a 1/10 chance that action A is played?

Keep in mind that one of the most basic ways to inspect whether in code is doing what you want is to test it.

So, you can answer your question by like this:

```auto
float ran = random(10);
println(ran);

```

> 7.702701

Or try printing a sample of outputs:

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

```

> 8.375377 5.7671003 0.24511933 1.3771093 6.604124 3.733101 2.9731035 2.9664493 5.653407 4.418477

…and, if you are unsure (maybe 7.7 == 7, or 8?), try it!

```auto
if (7.702701 == 7) {
  println("true");
}

if (7.702701 == 8) {
  println("true");
}

```

You will notice that warnings pop up in the PDE error box “dead code” – those will never be true.
