Random boolean values

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

1 Like

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

1 Like

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

1 Like

Hello,

This is not correct:

@freddukas,

Example:

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;
  }

:)

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

Example

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