# 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);
}
```

# Assignment

An open assignment for Tuesday.

Make a simple non-boring drawing application like auto-illustrator. You don’t have to make complex interfaces, just use the same tension between of randomness vs user interaction.
Bonus: making crazy random stuff is quite easy, try to make a tool that always produces good looking, or stylistically consistent imagery.

You can start from the bingo example above to produce unexpected event and from the simple drawing application we made last week:

```
//I declare a variable type boolean, I call it mouseIsDown
//and I assign a value false. All in one instruction.
boolean mouseIsDown = false;

void setup() {
size(500, 500);
background(255, 255, 255);
}

void draw() {

//variable stroke according to speed
//float strokeW = abs(mouseX - pmouseX);
//strokeWeight(strokeW);

//draw line only if mouse is down
if (mouseIsDown == true) //same as if(mouseIsDown)
{
line(mouseX, mouseY, pmouseX, pmouseY);
}

}

//this function is called every time you press a mouse button
void mousePressed() {
mouseIsDown = true;
}

//this function is called every time you release a mouse button
void mouseReleased() {
mouseIsDown = false;
}```
Posted in