Random elements different positions

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:

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);
    }
  }
}

I can’t run the code

widthGrid, heightGrid and other are not known

please check your post

Oops, I forgot those.

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