Constraint randomization of an array

In reply to mseyunni:
Let’s say you have

rand bit [3:0] arr[5];

This means that each element of
arr
can have the value 0-15. Since the sum() method returns a value that is the same type of each element, that means
arr.sum()
can only return a value between 0-15. However the maximum possible sum is 5x15=75 which requires at least 7 bits to represent. So you would have to write
arr.sum() with ( 7’(item) )
to get a result without overflowing. However, if if you constrain each element to 3 or less, the maximum sum is 5x3=15, and that does not overflow the element type.

1 Like