There are 2 other issues I missed ::
(1) Each bit of ‘N’ can be either 0 or 1 and you are trying to assign non-binary values ( 2 to 31 )
(2) There is a possible conflict when each index is being assigned twice
Eg: When i is 1, you constraint N[1] and N[2]
When i is 2, you constraint N[2] and N3]
N[2] is being constrained twice. If the constrained values are different there would be a conflict
This is a direct contradiction: N[2] cannot be both 1 and 2 simultaneously. The constraint solver detects this inconsistency and fails, resulting in the “constraint inconsistent” error.
///
class fin_array;
rand bit[31:0] N[10];
constraint c_N {
foreach (N[i]) {
if (i > 0 && i < 9) {
N[i] == i;
N[i+1] == i + 1;
}
}
}
endclass
module tb;
initial begin
fin_array f = new();
if (f.randomize()) begin
$display(“Randomization successful: N = %p”, f.N);
end else begin
$display(“Randomization failed!”);
end
end
endmodule
constraint c_N {
foreach (N[i]) {
if (i > 0 && i < 9) {
N[i] == i;
N[i+1] == i + 1;
}
}
}
endclass
module tb;
initial begin
fin_array f = new();
if (f.randomize()) begin
$display(“Randomization successful: N = %p”, f.N);
end else begin
$display(“Randomization failed!”);
end
end
endmodule