Unable to randomize multidimensional array

Hi,

I am trying to randomize a multi-dimensional array but it throws an error. Below is the code

class my_item;
  
  typedef enum {ADD, SUB, DIV, MOV} inst_e;

  rand int rand_arr[inst_e][inst_e];
  inst_e op;

  // Randomization constraints.
      constraint k2 {
        rand_arr.size() == op.num();
        foreach(rand_arr[i]) rand_arr[i].size() == op.num();
        
        foreach(rand_arr[i]) {
          rand_arr[i][ADD] == 0;
          rand_arr[i][SUB] == 1;
          rand_arr[i][DIV] == 2;
          rand_arr[i][MOV] == 3;
        }
      }  
  
endclass

module automatic test;
  
  function void run;
    my_item item = new();

    for (int i = 0; i < 1; i++) begin
      $display("NORMAL RANDOMIZE %0d", i);
      item.randomize();
    end
  endfunction
  
  initial run();
  
endmodule

Error: The constraint solver failed when accessing a null array rand_arr[0].
Please make sure array rand_arr[0] is allocated properly.

In reply to sj1992:

That’s not the right error message, tyour tool might have a problem.

The correct error message is the constraint rand_arr.size() == op.num(); cannot be met. That is because you cannot change the size of an associative array using a constraint. You need to allocate the array elements before calling randomize or in the pre_randomize function.

Since there is nothing random about this array, why are you trying to use randomize()?