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

In reply to poojashelge:

The first think you need to do is come up with a data structure that represents the answer you want to see. Then start applying the constraints you need one at a time and gradually refine them

module top;
  class A;
    rand bit [3:0] digit[10];
 
    constraint c  { 
      foreach (digit[i]) {
        // decimal 1st
        digit[i] <10; 
        // repetition 2nd
        i > 0 -> digit[i-1] != digit[i] ;
        // sequence 3rd
        i > 1 -> digit[i-2] != digit[i-1]+1 &&  digit[i-1]+1 != digit[i]+2 ;
      }
    }
  endclass
 
  A a=new;
      initial repeat (10) begin
    assert(a.randomize());
    $display("%p ",a.digit);
  end
 
endmodule


1 Like