I would like to see the first 3 elements of cnum to be set to 1 and rest to be 0. There is no real purpose here, I am just playing around with item.index usage. Another thing that I tried was item.index%2 and come up with alternate 1s and 0s. Constraint does not seem to be working for cnum, but, anum and bnum work just fine.
module test();
class temp;
rand bit anum[16];
rand bit bnum[16];
rand bit cnum[16];
constraint e {
bnum.sum() with (int'(item==0))==5;
anum.sum with (int'(item))==5;
cnum.sum with (int'(item.index<3?1:0))==3;
}
endclass
initial
begin
temp t = new();
t.randomize();
$display("%p\n %p\n %p", t.bnum, t.anum, t.cnum);
$finish;
end
endmodule
'{'h1, 'h1, 'h0, 'h0, 'h0, 'h1, 'h0, 'h0, 'h1, 'h1, 'h1, 'h1, 'h1, 'h1, 'h1, 'h1}
'{'h1, 'h1, 'h0, 'h0, 'h1, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h0, 'h1, 'h0, 'h1, 'h0}
'{'h0, 'h0, 'h1, 'h0, 'h1, 'h0, 'h1, 'h1, 'h0, 'h1, 'h1, 'h0, 'h1, 'h1, 'h1, 'h1} <<<— this is not right