# Random boolean values

**URL:** <https://discourse.processing.org/t/random-boolean-values/29191>\
**Category:** Coding Questions\
**Created:** [April 7, 2021, 3:56pm UTC](https://discourse.processing.org/t/random-boolean-values/29191 "2021-04-07T15:56:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![freddukas](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@freddukas](https://discourse.processing.org/u/freddukas)\
**Post date:** [April 7, 2021, 3:56pm UTC](https://discourse.processing.org/t/random-boolean-values/29191/1 "2021-04-07T15:56:31Z")

</div>

Hello, everyone.  
I need some random boolean variables to be changed in my draw fonction

Do i have a way to code a boolean variable randomly change ?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Tasperen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tasperen/32/12683_2.png) [@Tasperen](https://discourse.processing.org/u/Tasperen)\
**Post date:** [April 7, 2021, 5:03pm UTC](https://discourse.processing.org/t/random-boolean-values/29191/2 "2021-04-07T17:03:49Z")

</div>

You could do `boolean myValue = random(0,1);` since 0 is false and anything other than 0 is true. If you put that in the draw function, myValue is set to a either 0 or 1 depending on what value is generated during that loop

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 7, 2021, 5:49pm UTC](https://discourse.processing.org/t/random-boolean-values/29191/3 "2021-04-07T17:49:02Z")

</div>

I often find myself using `boolean b = random(1) < .5;`, and a cleaner take on that with the same idea, `boolean b = random(100) < 50;`.

---

<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:** [April 7, 2021, 5:50pm UTC](https://discourse.processing.org/t/random-boolean-values/29191/4 "2021-04-07T17:50:10Z")

</div>

Hello,

> [@Tasperen](#):
>
> You could do `boolean myValue = random(0,1);` since 0 is false and anything other than 0 is true

This is not correct:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bed2029aa7dd2096e2217fa718078081504127ce.png)

@freddukas,

Example:

```auto
void setup()
  {
  size(100, 100);
  frameRate(1);
  }

void draw()
  {
  boolean b = randomBoolean();  
  println(b);
  if (b)
    background(0);
  else
    background(255);
  }

boolean randomBoolean() 
  {
  return random(2) > 1;
  }

```

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 7, 2021, 5:50pm UTC](https://discourse.processing.org/t/random-boolean-values/29191/5 "2021-04-07T17:50:12Z")

</div>

```auto
if(random(1)<0.5) 
   boolMy=true; 
else boolMy=false;

```

**Example**

```auto
boolean boolMy=true; 

if (random(1)<0.5) 
  boolMy=true; 
else boolMy=false;

println(boolMy);

```

Please don’t start a new discussion for a similar question

when you have a few boolean, you can arrange them in a list / array

Also, to get a real answer show your entire code or the relevant lines you want to improve / work with.

Chrisir
