Randomize an array using constraints such that no number in the array repeats more than twice

This seems to work

module top;
 
  class A;
    rand bit [5:0] num[];
 
    constraint inside0_10 {
      num.size() inside{[30:50]};
    }
    constraint uniq { 
      foreach (num[i]) num.sum() with (6'(item==num[i]))<3;
    }
  endclass
 

  A a=new;
  initial begin
    assert(a.randomize());
    $display("%p ",a.num);
  end
 
endmodule
1 Like