Make only some random pixels change colour

elfelf
edited December 2015 in Questions about Code

Howdy!

So I'm an avid happy sonic media student and learning processing is part of an installation I want to make...

Here's my problem/question.

I have created some white noise, which I would like to decrease over time (change starting after 2 secs, intensifying after 10 secs etc), tending towards a black screen.

What I can't figure out is, how can I make only some (say, 50% of all pixels) random pixels change colour, while the rest is just black, within the same frame?

So far I could only make all of them change randomly, or all of them stay black. Any help would be much appreciated, thank you!!

void setup() {
  size(1000, 800);
}

void draw() {
  if (millis() < 2000) {
    loadPixels();
    for ( int i=0; i<pixels.length; i++)
      pixels[i] = color(random(255));
    updatePixels();
  }

  if (millis() > 2000) {
            loadPixels();
            for ( int i=0; i<pixels.length; i++)
              pixels[i] = color(random(255));
            updatePixels();
            loadPixels();
            for ( int j=0; j<pixels.length; j++)
              pixels[j] = color(0);
            updatePixels();
          }
        }
      }

    if (millis() > 10000) {
      loadPixels();
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color(random(255));
      updatePixels();
    }
  }
Tagged:

Answers

  • One empty line before, one after. Select the code, hit ctr-o. Code formatted.

    Similar problem

    https://forum.processing.org/two/discussion/13712/how-to-add-noise-to-the-image-being-drawn#latest

  • Thanks! Wasn't aware of the empty line before and after. Hmm, I tried playing around with the suggestions from the other thread, but I can't think of a way to apply it to this one. What I want is that for 50% of the pixels (randomly selected) it's: color(random(255)) For the other 50% it's color(0).

    Thanks again!

  • It is there, but here it is...

    int heads, tails;
    void draw() {
      if (random(1) >= 0.5) {
        println("Head");
        heads++;
      } else {
        println("Tail");
        tails++;
      }
    
      if (frameCount > 200) {
        println( heads + " heads and\n" + tails + " tails have been draw");
        exit();
      }
    }
    
Sign In or Register to comment.