# Random Square on a grid

Hi,

I’m trying to create a random grid, the cella size define the grid size.

I want to add a random movement, when the grid is 50 I want to move the rect one time on X and Y, but randomly (like a random walker).

Is this the correct mode ? Or there is something more elegant ?

``````int cella = 50;
int cx, cy;

void setup() {
size(500, 500);
background(0);

cx = width/2 - cella;
cy = height/ 2;
}

void draw() {
fill(0);
stroke(255);

//int cx = mouseX  / cella * cella;
//int cy = mouseY  / cella * cella;

float r = random(1);
if (r < 0.5) {
if (r < 0.25) {
cx += cella;
} else {
cx -= cella;
}
} else {
if (r < 0.75) {
cy += cella;
} else {
cy -= cella;
}
}

rect(cx, cy, cella, cella);
rect(width-cx-cella, cy, cella, cella);

if (cx >= width || cx <= 0 || cy <= 0 || cy >= height) {
cella =  (cella/2) / 5 * 5;
if (cella <= 5) cella = 250;
cx = width/2-cella;
cy = height/2;
}
}

``````

Sorry, I’m not at my computer right now, but does this code not run correctly? Or does it work fine, and you are asking if it can be improved?

It run, but with little bug on the grid cell because isn’t always on the perfect modulo.