How does noise remembers it's value.

edited February 2017 in Questions about Code

I'm wondering why noise(0) will give me the same value everytime when I run my sketch. But it doesn't as soon as I run it again. Meaning, noise function must be storing my values somewhere. Is that true?

My tessting code is:

float x;  
float y;

void setup() {
  size(800,600);  
  fill(255);
  noStroke();
  getRandom();
}  

void draw() {
  background(240,168,128);  
  ellipse(x, y, 20, 20);
}

void mouseClicked() {
  getRandom();
  println(x, y);
}

void getRandom() {
  x = noise(0) *200 + width/2;  
  y = noise(0) *200 + height/2; 
}

So, first time i run the sketh I get, let's say:

(565.2674, 465.2674)

And even clicking on screen the value stays the same. But as soon as I run the sketch again I get something else, like:

(494.57538, 394.57538)

Why is so?

Answers

  • Great, thanks. Then yes, the noise function stores a "seed".

    What about all the rest of the numbers? The depends directly on the seed? So, same number will always return same result, right?

    My test:
    float x;
    float y;

    void setup() {
      size(800,600);  
      fill(255);
      noStroke();
      getRandom(0); // first time, sets the seed
    }  
    
    void draw() {
      background(240,168,128);  
      ellipse(x, y, 20, 20);
    }
    
    void mouseClicked() {
      getRandom(0.05f); // not the seed  
    }
    
    void getRandom(float c) {
      x = noise(c) *200 + width/2;  
      y = noise(c) *200 + height/2; 
      println(x, y);
    }
    

    Returns:

    554.1805 454.18048

    550.33356 450.33356

    550.33356 450.33356

    550.33356 450.33356

    I'll assume yes, all the variations are coupled to seed.

  • Yes, the "seed" is a number being given to a pseudo-random number generator (PNRG). When given a seed the sequence of numbers it produces thereafter is deterministic -- same seed, same sequence.

    To learn more see:

    https://en.m.wikipedia.org/wiki/Pseudorandom_number_generator

    ...and read the collection of entries under the Random section of the Processing Reference:

    https://processing.org/reference/

  • Great. Thanks both. Also I found these crazy videos useful:

Sign In or Register to comment.