I am trying to make sure that the sum of elements in the arr is 10.
But somehow the randomization happening such a way that some of the elements in arr is negative (because of type int).
I will get the correct answer if I kept the constraint option
arr[i][j] < 10;
Is there any way to avoid this issue without the above constraint ?
class sample;
rand int arr[2][4];
constraint x {
foreach (arr[i, j]) {
arr[i][j] > 0;
//arr[i][j] < 10;
}
}
constraint xy {
arr.sum(row) with (row.sum(col) with ( int'(col) )) == 10;
//arr.sum() with (item.sum() ) == 10;
}
endclass
module top;
sample s = new();
int sum;
initial begin
s.randomize();
foreach (s.arr[i]) begin
$display("%p", s.arr[i]);
end
foreach (s.arr[i,j]) begin
sum = sum+s.arr[i][j];
end
$display("Sum =%0d", sum);
end
endmodule
result in eda playground is
'{1111106533, 65323945, 787707370, 750105959}
'{110764343, 273178680, 1188421186, 8359290}
Sum =10