# Random and Noise

Let’s add a little bit of uncertainty:

```void setup() { size(500, 500); frameRate(10); }   void draw() { background(random(255), random(255), random(255)); }```

What about creating ellipses in random positions?

```int myDiameter = 10;   void setup() { size(500, 500); background(0,0,0); noStroke(); smooth(); frameRate(10); }   void draw() { float posX = 250; float posY = 250; ellipse(posX, posY, myDiameter, myDiameter); }```

Make them appear only in a central square of 300×300! Random can accept a minimum and a maximum value.

```  fill(0,0,0,2); rect(0,0, width, height); fill(255, 255, 255); //back to white   float posX = random(100,400); float posY = random(100,400); ellipse(posX, posY, myDiameter, myDiameter);```

Try to restrict the color range!

Random can be used for triggering events every once in a while:

```  float bingo = round(random(1,60)); println(bingo);   if(bingo == 2) { //do something... }```

Random positioning vs random walk:

```  void setup() { size (300, 300); }   void draw() { background(200);   float rx; float ry; rx = width/2; ry = height/2;   rx = rx + random(-10, 10); ry = ry + random(-10, 10); ellipse(rx, ry, 40, 40); }```
```  float rx; float ry;   void setup() { size (300, 300); rx = width/2; ry = height/2; }   void draw() { background(200); rx = rx + random(-10, 10); ry = ry + random(-10, 10); ellipse(rx, ry, 40, 40); }```

# Noise

Perlin noise is an infinite random sequence of values changing harmonically. It is the basis of a lot of special effects and procedural computer graphics and it’s much more interesting than random.

The sequence will appear “natural” if you take contiguous samples (xoff). For advanced use check the reference.

```//the sampled coordinate float xoff = 0.0;   //stores the position of the random circle float randomPosition = 150;   void setup() { size(300, 300); smooth(); }   void draw() { background(0);   //ellipse moving randomly randomPosition += random(-4, 4); ellipse(randomPosition, 100, 10, 10);   xoff = xoff + .01; float n = noise(xoff) * width; ellipse(n, 200, 10, 10); }```
Posted in