# Random elements different positions

**URL:** <https://discourse.processing.org/t/random-elements-different-positions/16984>\
**Category:** Coding Questions\
**Created:** [January 9, 2020, 4:52pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984 "2020-01-09T16:52:19Z")\
**Posts on this page:** 5\
**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:** [January 9, 2020, 4:52pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984/1 "2020-01-09T16:52:19Z")

</div>

Hi

I want to draw squares on random positions on a grid, but I don’t want to have them on the same position. If I run the code sometimes there are two squares on the same position. I know that it has something to do with positionSquares.

This is what I have now:

```auto
int widthGrid = 40;
int heightGrid = 40;
int heightBlock = 20;
int widthBlock = 20;
int Square[][] = new int[widthGrid][heightGrid];
int blockX[] = new int [widthGrid];
int blockY[] = new int [heightGrid];
int xPositionBlock;
int yPositionBlock;

void setup() {
  size(1000, 1000);
  positionGrid();
  positionSquares();
}

void draw() {
  drawGrid();
  drawSquares();
}

void positionSquares() {
int i = 0;
  while(i < 15){
    Square[i][0] = blockX[(int)random(widthGrid)] * widthBlock;
    Square[i][1] = blockY[(int)random(heightGrid)] * heightBlock;
    i++;
  }
}

void drawSquare(int i) {
  fill(#ff0000);
  rect(Square[i][0], Square[i][1], widthBlock, heightBlock);
}

void drawSquares() {
  for (int i = 0; i < 15; i++) {
    drawSquare(i);
  }
}

void positionGrid() {
  for (int i = 0; i < widthGrid; i++) {
    blockX[i] = i;
  }
  for (int i = 0; i < heightGrid; i++) {
    blockY[i] = i;
  }
}

void drawGrid() {
  stroke(0);

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

```

---

<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:** [January 9, 2020, 7:33pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984/2 "2020-01-09T19:33:41Z")

</div>

I can’t run the code

widthGrid, heightGrid and other are not known

please check your post

---

<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:** [January 9, 2020, 7:46pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984/3 "2020-01-09T19:46:22Z")

</div>

Oops, I forgot those.

I edited the post. Hopefully u can run it now.

---

<div class="post-metadata">

**Author:** ![Dimdimtri](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@Dimdimtri](https://discourse.processing.org/u/Dimdimtri)\
**Post date:** [November 27, 2020, 5:50pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984/4 "2020-11-27T17:50:10Z")

</div>

Hello San , did you find the solution ? Because i also try to put rectangles at random but only use a number once.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 28, 2020, 6:33pm UTC](https://discourse.processing.org/t/random-elements-different-positions/16984/5 "2020-11-28T18:33:17Z")

</div>

You can do this by creating a vector array and every time you create a random x-y value cheque in the array if it doesn’t already exist, meaning not 0. When not, draw the square and save the position in the array.

> **[Array \\ Language (API) \\ Processing 3+](https://www.processing.org/reference/Array.html)**
