Constraint on at least N unique digits in array

Hi,

Came across an interesting constraint requirement that I can’t come up with beautiful solution.

for a fixed size array, each elements should be inside 0-9; and there must be at least 7 unique elements in this array.

what’s the best way to interpret the unique requirement here?

int num[10];

constraint is_unique{
  foreach(num[i]) num[i] inside{[0,10]};
}


what I can think of is use a function to calculate number of unique elements; But I remember for functions inside constraint , it would imply constraint are only one direction, not sure if this will affect the solution space?

In reply to Jeff_Li_90:

Use helper arrays

module top;

  class A;
    rand bit [3:0] num[10];
    rand bit [3:0] unum[7]; // helper
    
    constraint inside0_10 {
      foreach(num[i]) num[i] inside{[0:10]};
    }
    constraint uniq { unique {unum};
                     foreach (unum[i]) num.sum() with (4'(item==unum[i]))==1;
                    }
  endclass

  A a=new;
  initial repeat(10) begin
    assert(a.randomize());
    a.num.sort();
    a.unum.sort();
    $display("%p %p",a.num, a.unum);
  end
                                                       
endmodule

In reply to dave_59:

should this be following?


foreach (unum[i]) num.sum() with (4'(item==unum[i])) >= 1;

In reply to Jeff_Li_90:

You should try it.

In reply to dave_59:

Hi Dave ,
I did not follow what is implied by this “foreach (unum[i]) num.sum() with (4’(item==unum[i]))==1;”

Thanks in Advance.

In reply to CPU_Verif:

I did not follow what is implied by this “foreach (unum[i]) num.sum() with (4’(item==unum[i]))==1;”

“there must be at least 7 unique elements in this array”

You should unroll the foreach loop into 7 constraint expressions and see if that helps you see what it does. If that is not enough, unroll one of the num.sum() methods.

In reply to dave_59:

Thank you Dave , My bad I wrongly Assumed we neeed 7 unique elements && 3 same elements
For what I had in my mind added one more check to see that the other 3 elements are same

   rand bit [3:0] constval;
....
    constraint uniq { unique {unum};
                     foreach (unum[i]) num.sum() with (4'(item==unum[i]))==1;
                     num.sum() with (4'(item==constval))==3; // Make sure three elements are similar 
                     
                    }

now clear thank u :)

In reply to CPU_Verif:

The 3 other elements do not need to be the same. the requirement was “at least 7 unique elements”. So it is OK to have 10 unique elements.