What does this mean.?

Hi all,

what does the folowwing code mean?

if(index1 > 7 && random(2) == 0) index1 = 0;        
  if(index2 > 7 && random(3) == 0) index2 = 0;

  strip.setPixelColor(index1, 255, 0, 255);
  strip.setPixelColor(index2, 255, 0, 255);

look at it formatted properly

{
    if(index1 > 7 && random(2) == 0)
        index1 = 0;
    if(index2 > 7 && random(3) == 0)
        index2 = 0;

    strip.setPixelColor(index1, 255, 0, 255);
    strip.setPixelColor(index2, 255, 0, 255);
}

Break it down…

if(index1 > 7 && random(2) == 0)
   index1 = 0;        

if(index2 > 7 && random(3) == 0)
   index2 = 0;

Welcome to the forum.

First have a look at the Operator Precedence

https://en.cppreference.com/w/cpp/language/operator_precedence

This tells you the logical AND (&&) is evaluated last.

Lets look at the random function

https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

random(2) will return 1 or 0 so there is a 50% chance of that being true,

Now combine the two statements with AND ( both have to be true ).

So, if index1 is larger than 7 with a 50% chance, index1 is set to 0.

Whatever it means might make a lot more sense than it does tots out of any context.

Could you tell us what the sketch does and like, I dunno, post it here?

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.