# Randomized lines on a grid - homework help - it's kinda due tonight

**URL:** <https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 20, 2021, 7:01pm UTC](https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964 "2021-10-20T19:01:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamii](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Jamii](https://discourse.processing.org/u/Jamii)\
**Post date:** [October 20, 2021, 7:01pm UTC](https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964/1 "2021-10-20T19:01:41Z")

</div>

Hi, I’m really new to processing and I need some help on an idea I don’t really know where to start at.

What I have now is a grid, and I’ll make it not visible later, it just acts as a guideline.

I want a vertical line to appear in every single box of the grid, with randomized thickness and location inside each grid box. I don’t think I have the right general idea to pull this.

so would I was thinking I code a line appearing randomly in one square with different thickness first, then loop it down the x-axis then the y-axis of the grid.

Is there another method or idea anyone would suggest?

ah- sorry if it’s hard to read, I don’t know how to upload what I have so I just copied pasted it directly in text from processing.

int x;  
int y;

int gridX = 20;  
int gridY = 30;

int gridXB = 120;  
int gridYB = 120;

void setup()  
{  
size(1200, 900);  
background(230);  
noStroke();  
frameRate(10);  
}

void draw()  
{  
rectMode(CENTER);  
noFill();  
stroke(0);  
strokeWeight(1);  
rect(width/2, height/2, 1100, 800);

//first square starts within 100 from the boarder (20+80)  
//grid squares end before the boarder (120 away from screen size)  
for (int x = gridXB; x \<= width-120; x += gridX)  
{  
for (int y = gridYB; y \<= height-120; y += gridY)  
{  
strokeWeight(0.25);  
noFill();  
rect(x, y, 19, 29);

```
  //the line randomly generates within each square of the grid
  //the line varies from different thickness
  //looping from the first line down the x-axis
  }
}

```

{

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2021, 2:33pm UTC](https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964/2 "2021-10-26T14:33:54Z")

</div>

when you want a line within each cell of your grid you’re almost there:

for example

```auto
strokeWeight(random(1,8)); 

line (x+3, y, x+3, y+gridY-1);

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 26, 2021, 2:48pm UTC](https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964/3 "2021-10-26T14:48:29Z")

</div>

Hello,

> [@Jamii](#):
>
> ah- sorry if it’s hard to read, I don’t know how to upload what I have so I just copied pasted it directly in text from processing.

How to _Format Your Code_:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 26, 2021, 4:55pm UTC](https://discourse.processing.org/t/randomized-lines-on-a-grid-homework-help-its-kinda-due-tonight/32964/4 "2021-10-26T16:55:40Z")

</div>

or since you use

` rectMode(CENTER);`

try

```auto
     strokeWeight(random(1, 18)); 

      line (x, y-gridY/2+2, 
        x, y+gridY/2-2);

```

* * *

**Only ONE line**

when you want to show only one line that’s moving:

You can count the current cell within the for-loop just by adding 1 to a counter named currentCell (add 1 within the for-loop).

Also make a counter2++ at the very end of draw() where you store in which cell the line has to appear.

Surround the drawing line() command above with

```auto
if(currentCell==counter2) {
    ...
}

```

Init both variables before setup() and set currentCell to 0 before the for-loops.
