Randomizing 2D int arrays producing unexpected results

There are 2 things you need to do

  • Declare the array elements with an unsigned type. Either int unsigned or bit [N:0]
  • The sum() needs to be calculated in a context with no overflow. With 8 elements in the array, you need at least 3 extra bits.
class sample;
  rand bit[3:0] arr[2][4]; // implicitly constrained to values 0-15 
 
  constraint xy {
    arr.sum(row) with (row.sum(col) with ( 8'(col) )) == 10;
  }
endclass

See How does .sum() operate in a constraint