Square matrix of size odd number and no duplicate max element in rows

In reply to sra1dreddy:

A problem with array locater methods like max() is they return a queue, not a single value. This is in case the array is empty to begin with. That means you cannot not use locator methods in constraints. In this example, it is fairly easy to work around by adding a helper array: max_value.

class matrix_sq;
  rand bit [3:0] md_array[][];
  rand bit [3:0] odd_num;
  rand bit [3:0] max_value[];
  constraint c_md_array { 
    odd_num % 2 == 1; odd_num > 1;
    
    // First assign the size of the first dimension of md_array
    md_array.size() == odd_num;
    max_value.size() == odd_num;
 
    foreach (md_array[r]) {
      // make the array square
      md_array[r].size() == odd_num;
      // one and only one max value in a row
      md_array[r].sum() with (int'(max_value[r] == item)) == 1;
     }
     // compute max_values and make unique
     // this works because the previous constraint makes sure at least one element
     // is equal to the max value
    foreach (md_array[r,c]) {
      max_value[r] >= md_array[r][c];
    }
    unique { max_value };
  }
endclass
 
module tb;
 
  initial repeat (5) begin
   static matrix_sq sq = new;
    assert(sq.randomize());
    $display ("%p", sq);
  end
endmodule