Constraint randomization of an array

In reply to dave_59:

Thanks for the reply Dave. Oddly enough I cannot reproduce the error. It seems to be working. And thanks for showing how to shorten the constraint.

@dave_59, I am trying to randomize the array of any size. However I get compile error, can you please let me know where i am going wrong in this.

Here is my code :

class array_matrix #(M, N);   // generate a array of size MxN
  rand int array[][];
  
  constraint size {
    array.size() == MxN;   // size should be mulitple of the matrix MxN
    
    array.sum() with (32'(item)) == {[MxN]} ;  // sum should be = matrix MxN
    
    foreach (array[i][j])
        array[i][j] inside {[MxN]};  // each element should be inside the MxN var.
}

 constraint uni {
    unique {array[i][j]}; }  //have unique elements in the array
  }
  
  function void print();
    foreach(array[i][j]) 
      $write("\n Array[i][j]=[%0d][%0d], ", i,j,array[i][j]);
    $display("\n =============================");
  endfunction
  
endclass

module const_ex;
  array_matrix arr1;
  
  initial begin
    arr1 = new;
    void'(arr1.randomize());
  end
endmodule