Systemverilog constraints

In the below transaction class definition, write your own logic in the space provided for you
to meet the following requirements. –
a. The dynamic array should contain 20 items.
b. Out of 20 items, the values of 3 items should be equal to 5 at random positions.
c. The values of 5 items must be 10 at random positions
d. The values of 8 items must be 15 and the remaining 4 items must be 20.
Note : Please don’t use distribution constraint, as it will not really meet this requirement &
post randomize method

I will give you a hint. Requirement b can be stated by

constraint b { array.sum() with {int'(item==5)} == 3; }
class packet;
  int array[];
  constraint s{array.size==20;}
  constraint b { array.sum() with (int'(item==5)) == 3; }
  constraint c { array.sum() with (int'(item==10)) == 5; }
  constraint d { array.sum() with (int'(item==15)) == 8; }
  constraint e { array.sum() with (int'(item==20)) == 4; }
endclass



module tb;
  packet h;
  initial 
    begin 
      h=new();
      assert(h.randomize);
      $display("array=%p",h);
      end
endmodule

simulation result getting
array=‘{array:’{}}

You need to declare array with rand

rand int array[];

Hi,

Why is it ==3 and not ==15? (15 being the sum of 3 elements equal to 5)
Doesn’t sum() return the sum of array elements?

The requirement is:

We are constraining the count of items with the value 5, not the the sum of the items. The sum() method returns the sum of the iterative expressions inside the with () clause.