# 10 Random red blocks on a grid

**URL:** https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814
**Category:** Coding Questions
**Created:** [November 25, 2019, 8:55pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814 "2019-11-25T20:55:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![San](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@San](https://discourse.processing.org/u/San)
#### Post date: [November 25, 2019, 8:55pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/1 "2019-11-25T20:55:14Z")

</div>

Hey!  
I made this 60x40 grid and I am trying to make 10 of the squares red. And it has to be 10 random squares each time I run the program. I have tried a lot but can’t figure it out…  
Can anyone please tell me who to do it?

```auto
int widthGrid = 60;
int heightGrid = 40;
int block[][] = new int[widthGrid][heightGrid];

int widthBlock = 10;
int heightBlock = 10;

void setup() {
  size(600, 400);
}

void draw() { 

  for (int i = 0; i < widthGrid; i++) {
    for (int j = 0; j < heightGrid; j++) {
      int xPositionBlock = i * widthBlock;
      int yPositionBlock = j * heightBlock;

      fill(#0ff09d);
      rect(xPositionBlock, yPositionBlock, widthBlock, heightBlock);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![unique\_username2](https://avatars.discourse-cdn.com/v4/letter/u/aca169/32.png) [@unique\_username2](https://discourse.processing.org/u/unique_username2)
#### Post date: [November 25, 2019, 9:26pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/2 "2019-11-25T21:26:57Z")

</div>

You could try using a different array with values of the x and y coordinates of the grid(by tens, so that it stays within the correct position on the grid, as the size of the squares are ten), and making a _block_ class, maybe something like this;  
NOTE; remember to myBlockN.display(); in void draw for all 10

```auto
int widthGrid = 60;
int heightGrid = 40;

int blockX[] = {
0,10,20,30,40,50
};
int blockY[] = {
0,10,20,30
};

int widthBlock = 10;
int heightBlock = 10;

class Block() {
color c;
int xpos;
int ypos;
Block(tempxpos, tempypos, tempc) {
xpos = tempxpos;
ypos = tempypos;
c = tempc;}

void display() {
fill(c);
rect(xpos,ypos,10,10);
}
}

block myBlock1;
block myBlock2;
block myBlock3;
block myBlock4;
block myBlock5;
block myBlock6;
block myBlock7;
block myBlock8;
block myBlock9;
block myBlock10;

void setup() {
size(600,400);
myBlock1 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock2 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock3 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock4 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock5 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock6 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock7 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock8 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock9 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock10 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
}

```

---

<div class="post-metadata">

### Author: ![San](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@San](https://discourse.processing.org/u/San)
#### Post date: [November 25, 2019, 9:54pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/3 "2019-11-25T21:54:26Z")

</div>

Thank you!  
I changed it up a little since I don’t want to use class. But it works now!  
Sometimes if I run the program there are two red blocks on the same position so I only see 9 blocks…  
It doesn’t happen often but if I increase the number of red blocks to 30 it does happen a lot.  
I tried to fix that with an ‘if-statement’ but I can’t get it to work…

```auto
int widthGrid = 60;
int heightGrid = 40;
int block[][] = new int[widthGrid][heightGrid];
int blockX[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60
};
int blockY[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
};
int widthBlock = 10;
int heightBlock = 10;

int positionBlockY;
int positionBlockX;

void setup() {
  size(600, 400);
}

void draw() { 

  for (int i = 0; i < widthGrid; i++) {
    for (int j = 0; j < heightGrid; j++) {
      int xPositionBlock = i * widthBlock;
      int yPositionBlock = j * heightBlock;
      fill(#0ff09d);
      rect(xPositionBlock, yPositionBlock, widthBlock, heightBlock);
    }
  }
  noLoop();

  for (int p = 0; p < 10; p++) {
    positionBlockX = blockX[(int)random(60)] * 10;
    positionBlockY = blockY[(int)random(40)] * 10;
    println(positionBlockX, positionBlockY);
    fill(#ff0000);
    rect(positionBlockX, positionBlockY, widthBlock, heightBlock);
  }

```

}

---

<div class="post-metadata">

### Author: ![unique\_username2](https://avatars.discourse-cdn.com/v4/letter/u/aca169/32.png) [@unique\_username2](https://discourse.processing.org/u/unique_username2)
#### Post date: [November 25, 2019, 9:57pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/4 "2019-11-25T21:57:10Z")

</div>

Maybe you could add something that detects whether or not a space is taken, and do something similar to  
positionBlockX = blockX[(int)random(60),!=\*taken blocks] \* 10;  
?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 26, 2019, 12:05am UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/5 "2019-11-26T00:05:18Z")

</div>

Please use the following way to set arrays with more than a couple values (if it‘s possible) :

```auto
int[] blockX;

void setup() {
   blockX = new int[61];
   for (int i = 0; i < blockX.length; i++) {
      blockX = i;
   }
}

```

---

<div class="post-metadata">

### Author: ![San](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@San](https://discourse.processing.org/u/San)
#### Post date: [November 26, 2019, 9:05am UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/6 "2019-11-26T09:05:00Z")

</div>

Haha… Thanks 🙂

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [November 30, 2019, 9:06pm UTC](https://discourse.processing.org/t/10-random-red-blocks-on-a-grid/15814/7 "2019-11-30T21:06:24Z")

</div>

If you are picking things at random (and then checking to see if you have already picked them) it can get very inefficient. You might want to pick 9 unique values out of 10, but on average you will have to make 19 random selections before you get 9 unique ones.

One way around this is to shuffle, then read n numbers from the top of the shuffled list. Another way is to create a list, then pop random values off as you pick each one. Either of these approaches can be done with IntList:

[https://processing.org/reference/IntList.html](https://processing.org/reference/IntList.html)
