Randomizing a dynamic array size

In reply to rhariprasad:

It seems “sum” method treats every array’s element with signed value. When you calculate them with signed value, the result’s always correct (= 50).

To fix this, you can cast every element of array in sum function with unsigned value:


typedef int unsigned uint_t;
rand bit [7:0] a[$];
constraint SIZE_SUM {
  a.size() == 3;
  a.sum(x) with(uint_t'(x)) == 50;
}

Or add more constraint for every element of array:


rand bit [7:0] a[$];
constraint SIZE_SUM {
  a.size() == 3;
  a.sum() == 50;
  foreach(a[i]) {
    a[i] inside {[0:50]};
  }
}