Random

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);

Add a random fill!
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);
}
Posted in

Post a comment