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

How do I randomize an array using constraints such that no number in the array repeats more than twice?
If I knew that a particular number (say 2) should not repeat more than 2 times, I could do as shown below:

constraint C{addrs.sum(item) with (int'(item)==2) < 6 ;}

But how can this be generalized for any element in the array?

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

In reply to Tr0408:

BTW, the cast 6’(item==num[i]) only works because num.size() is limited to 50. It’s unrelated to the with of num elements. I would use int instead for clarity and you don’t have to worry about negative sizes.

Write a program to generate a 10-digit decimal number using constraint such that no
repetition of digits of more than 2(so 33 is allowed 333… is not) and no sequence of more than
3(234 is allowed but 2345 is not

i am unable to get this please help

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


In reply to dave_59:

Hello Sir,
Could you explain how this goto operator worked inside a constraint and what those last statements (conditions) mean?
Never seen such a constraint format before.
And using the second bracket {}, can we really use this parenthesis in case of loops in SV?

And one more thing. Could you tell me how to go with assertions? I have been on this topic for the past couple of weeks. Still, it feels like I know nothing whenever I see any tricky assertion-based interview question.

In reply to dave_59:

Hello Sir,
I think there are some mistakes in your code.

  1. Question demands to have a constraint that will make sure that any particular digit does not appear more than 2 times consecutively.
    But the line you have written it’s assuring that any digit does not occur consecutively.
  2. Another condition is like 2 3 4 is valid but 2 3 4 5 is not. Your code is checking something
    else. ( digit[i-1]+1 != digit[i]+2 ) - why has this condition been made ?

I tried to make tiny little changes (using the if statement) in your code. Neither am I sure of my code is accurate and covers all cases nor am I getting your code.
Please look into it.


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] ;
        if(i>1 && digit[i]==digit[i-1])
          digit[i]!=digit[i-2];
        // sequence 3rd
        //i > 1 -> digit[i-2] != digit[i-1]+1 &&  digit[i-1]+1 != digit[i]+2 ;
        if(i>2 && digit[i]==digit[i-1]+1 && digit[i]==digit[i-2]+2)
          digit[i]!=digit[i-3]+3;
       //if(i>)
      }
    }
  endclass
 
  A a=new;
      initial repeat (5) begin
    assert(a.randomize());
    $display("%p ",a.digit);
  end
 
endmodule

vsim -voptargs=+acc=npr

run -all

'{5, 2, 3, 0, 3, 2, 9, 3, 3, 1}

'{2, 8, 9, 9, 8, 6, 9, 2, 2, 3}

'{2, 1, 5, 1, 2, 0, 9, 9, 4, 6}

'{8, 3, 1, 0, 9, 3, 6, 5, 0, 2}

'{3, 1, 6, 3, 7, 1, 6, 8, 6, 3}

exit

End time: 07:33:47 on Oct 14,2022, Elapsed time: 0:00:01

Errors: 0, Warnings: 0

In reply to Shubhabrata:

It depends on which question you are looking at; the one from Tr0408 or poojashelge. The latter was about repetition. That is why it is much better to ask a new question rather than replying to an old question with a new question.

→ in the context is boolean implication, nit the goto operator which must be inside square brackets [->1]