Constrain sum of elements in an array

In reply to naveensv:

Naveen,

I think you meant to declare a dynamic array:

rand bit [2:0] a[];

And you would have to add at least 2 bits to catch the error with the given constraints.

What you are trying to explain is that the result of the array.sum() method only has the precision of the individual array elements. But there is no need to add extra bits to the array, you can use the with clause to cast the elements to a greater width.

a.sum(item) with ( 32'(item) )