# Random Square on a grid

**URL:** <https://discourse.processing.org/t/random-square-on-a-grid/3382>\
**Category:** Coding Questions\
**Created:** [September 8, 2018, 9:43pm UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382 "2018-09-08T21:43:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeremTurkyilmaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keremturkyilmaz/32/4433_2.png) [@KeremTurkyilmaz](https://discourse.processing.org/u/KeremTurkyilmaz)\
**Post date:** [September 8, 2018, 9:43pm UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382/1 "2018-09-08T21:43:34Z")

</div>

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 ?

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [September 8, 2018, 10:40pm UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382/2 "2018-09-08T22:40:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KeremTurkyilmaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keremturkyilmaz/32/4433_2.png) [@KeremTurkyilmaz](https://discourse.processing.org/u/KeremTurkyilmaz)\
**Post date:** [September 8, 2018, 10:44pm UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382/3 "2018-09-08T22:44:09Z")

</div>

It run, but with little bug on the grid cell because isn’t always on the perfect modulo.  
But yes is about improvement.

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [September 8, 2018, 11:34pm UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382/4 "2018-09-08T23:34:51Z")

</div>

What do you mean by random grid; random grid size or random positions?

---

<div class="post-metadata">

**Author:** ![KeremTurkyilmaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/keremturkyilmaz/32/4433_2.png) [@KeremTurkyilmaz](https://discourse.processing.org/u/KeremTurkyilmaz)\
**Post date:** [September 9, 2018, 9:02am UTC](https://discourse.processing.org/t/random-square-on-a-grid/3382/5 "2018-09-09T09:02:28Z")

</div>

The grid start with a size of 50, but when the rect touch the border it’s divide by itself and restart on the middle. I want to move the rect by random position, but not on random width or height but moving one step left or right, up or down.
