Random object of four in each square of a grid

I feel like I should know the answer to this or figure it out but I feel stuck.

I am drawing a grid, let’s say 4x4, and I have 4 different shapes or items that I want to draw. Each frame I want each square of my grid to be populated with one of the 4 items at random.

Would I use modulo? If/else? Something different? If someone could point me in right direction that would be super helpful!

// I am drawing a grid.
  //  It has four rows.
  for( ??? ; ??? < ??? ; ??? ){
    // Each row has four squares.
    for( ??? ; ??? < ??? ; ??? ){
      // Draw the grid square.
      rect( ???, ???, ???, ???);
      // Each square has a random shape.
      int ??? = random( ??? );
      // If the random shape is a TYPE_1 shape,
      if( ??? == ??? ){
        // Draw TYPE_1 shape.
        ???( ???, ???, ???, ??? );
      }
      // If the random shape is a TYPE_2 shape,
      if( ??? == ??? ){
        // Draw TYPE_2 shape.
        ???( ???, ???, ???, ??? );
      }
      // Etc for TYPE_3 and TYPE_4 shapes.
      // ???????????...
    }
  }
1 Like

Hello @i-draw-monkeys,

Master the for() loop and then progress to the nested for() loop.

Take a look through the resources here:

:)

Hello @i-draw-monkeys :slightly_smiling_face:

Modulo is good for when you want to create a regular (predictable) pattern.

Since you want a random pattern, I would use a switch statement. Assign each shape a number of 0, 1, 2, or 3. And then use the random function to select the shape.

You can read how to use the switch statement here:

:nerd_face:

2 Likes

Sorry I should have been more clear, I know how to make the grid. The problem is varying the objects!

1 Like

I tried this—the problem is that some squares come up blank. I don’t understand why. I am substituting simple rectangles in my code for the shapes, here is the example:

int diameter;


void setup() {
  size(960, 1152);
  diameter = 192;
  frameRate(1);
}

void draw () {
  background(255);


  for (int g=0; g < width; g=g+diameter) {
     int num =int(random(4));
    for (int h = 0; h <height; h=h+diameter) {
    
     float gen = random(8); 
   if (gen >= 0 && gen <=2){
        type1(g, h);
   } else if (gen > 2 && gen <= 4){
        type2(g, h);
   }
      else if (gen > 4 && gen <=6){
        type3(h, g);
   } else if (gen > 6 && gen <=8){
        type4(h, g);
   }
    }
  }
}
     
void type1(int a, int b) {
  
  pushMatrix();
  fill(100, 120, 11);
  rect(a+30,b+30, 132,132);
  popMatrix();
}

void type2(int a, int b) {
  
  pushMatrix();
  fill(0, 255, 5);
  rect(a+30,b+30, 132,132);
  popMatrix();
}

void type3(int a, int b) {
  
  pushMatrix();
  fill(255, 0, 120);
  rect(a+30,b+30, 132,132);
  popMatrix();
}

void type4(int a, int b) {
  
  pushMatrix();
  fill(30, 40, 120);
  rect(a+30,b+30, 132,132);
  popMatrix();
}

I tried this last night actually after posting the question. It didn’t fully work and I am not sure why as it seemed like it should be the solution. But now I just retried it and boom! It works ha. Okay, now I will translate it all back to my original code and fingers crossed this still works.

for (int g=0; g < width; g=g+diameter) {
     int num =int(random(4));
    for (int h = 0; h <height; h=h+diameter) {
    
      int gen = int(random(4));
      switch(gen) {
        case 0:
        type1(g, h);
        break;
        case 1:
        type2(g, h);
         break;
         case 2:
         type3(g, h);
         break;
         case 3:
         type4(g, h);
         break;
      }

Okay, I went back to my original code and I had transposed x,y in a couple shapes and it was making it look like it wasn’t working but it was :upside_down_face:

3 Likes

In the old version you had sometimes
swapped h and g…

1 Like

This is not in use…?

1 Like

oh yeah remnants of old attempts…I should have erased before putting up

1 Like

yeah this is my messiness, and it was an error I made in the original code…

1 Like

You’ve made good use of the random() function already. It has the feature that you never know if all 4 of the shapes will turn up, or how many of them will appear in a given run of 16.

One interesting way of controlling the number of shapes is to use an IntList. Initialize it with the numbers you want, then shuffle it (there’s a handy method to do just that). Step through the IntList to take the numbers out in random order. You can even play with probabilities by loading the list with more than 16 numbers, but just taking out the first 16.

I’ve found that shuffling lists can offer a good mix of control and randomness.

3 Likes

Good point.

Using random, a number could be missing, shuffle is better.

Also instead of a low frameRate we could also use a timer to change the pattern every 2 seconds or so

1 Like

Why not use the IntList shuffle function?