code to randomise whether to mute or unmute song playing.

edited June 2016 in Library Questions

Hi there,

I've been trying to work out how to do what the title says.

Basically, I have 4 instruments playing at the same time that make a song when played together, each instrument is played through a different song, so I can control each instrument individually. I want it so, when the user clicks there mouse, the instruments randomly mute or stay playing, or if they are already not playing, they either keep not playing, or become unmuted.

Any help would be greatly appreciated.

Thanks.

Tagged:

Answers

  • edited June 2016

    This will give you an array of 4 randomly generated booleans, which you can use to set mute or play. You could put this in your mouseClicked() routine. Change the 2 in random and/or > 1 to change the probability of getting a true.

        boolean[] play = new boolean [4];
    
        void setup() {
        }
    
        void draw() {
          for (int i = 0; i < 4; i ++) {
            if (random(2) > 1) {
              play[i] = true;
            } else {
              play[i] = false;
            }
            println(play[i]);
          }
    
          delay(500);
        }
    
Sign In or Register to comment.