Constraint for repeating numbers in an array in a unique pattern

In reply to dave_59:

In reply to kernalmode1:
Was not entirely clear if length and k were set before calling randomize, but this should work in most cases.

module top;
class C;
rand bit[4:0] A[], uniq_A[];
rand bit [7:0] length,k;
constraint sizes { 
length == A.size();
length % k == 1;
length / k == uniq_A.size();
}
constraint uniqueness {
unique {uniq_A}; 
foreach(uniq_A[i]) A.sum() with (int'(item==uniq_A[i])) == k;
}
endclass
C c = new();
initial repeat(5) begin
assert(c.randomize() with {length == 19;});
c.A.sort();
$display("%p", c);
end
endmodule

Hi Dave

I understand your solution. I also solved this using post_randomize. Is this better than doing in post_randomize as kernal1mode suggested? or both ways can be used interchangeably.