In reply to mseyunni:
One problem is that your constraints create an irregularity shaped array with each row potentially having a different number of columns. You’ll get index range errors if the i+1 row has fewer columns than the row i. If the constraints fail, the solver leaves the array values unchanged. You should always check the return value from a call to randomize(). You should randomize the number of rows and columns as separate variables and then constrain the array to those variables.
Your general approach only checks that the values within each row and each column are unique, not unique within the entire array. It’s possible that there are many other non-unique values in the array; it’s just that the 0 values are easier to pick out. What you need to do is a nested set of foreach loops to compare every element with every other element.
To print the array as a matrix, you need a double nested for each loop and use the $write function which does not append a newline to the end of each output statement.
class array2d;
rand bit[10:0] array2d [] [];
rand int rows, cols;
constraint sizes {
rows inside {[10:20]}; cols inside {[5:10]};
array2d.size() == rows;
foreach(array2d[ii])
array2d[ii].size == cols;
}
constraint c_array2d_elem {
foreach(array2d[i1,j1])
foreach(array2d[i2,j2])
i1!=i2 || j1!=j2 -> array2d[i1][j1] != array2d[i2][j2];
}
function void print();
$display("size of matrix rows: %0d cols: %0d", rows, cols);
foreach(array2d[i]) begin
foreach(array2d[i][j])
$write("%5d ", array2d[i][j]);
$display;
end
endfunction
endclass
module top;
array2d a = new;
initial repeat(5)
assert(a.randomize) a.print;
endmodule