Sum of array (array constraints)

I have the code for the following constraint:

  1. Sum of all array elements must be 17
  2. Array must have at least one element which is multiple of 4.
  3. Size of an array can be anything
class Example;
  rand bit [15:0] my_arr[]; //if I change to 31 here, I see an incorrect array 
  rand int inx_num;
   
   constraint c1 {my_arr.size() inside {[10:15]};}
   constraint c1a {inx_num inside {[0:my_arr.size()]};}
   constraint c2 {foreach (my_arr[i]) {
     i == inx_num -> my_arr[i] % 4 == 0 && my_arr[i] > 4;
   }};
                  
   constraint c3 {my_arr.sum() with(int'(item))== 17;}
                  
   function void post_randomize();
     $display("my_arr = %p \n", my_arr);
   endfunction
                  
 endclass
                  
 module TB;
   Example E;
   
   initial begin
     E = new();
     if(!(E.randomize()))
       $display("error");
   end
 endmodule

In line #2:
rand bit [15:0] my_arr[] → gives me the correct array; like this:
my_arr = '{'h0, 'h1, 'h1, 'h2, 'h1, 'h0, 'h0, 'h1, 'h3, 'h0, 'h8}

rand bit [31:0] my_arr[] → gives me an incorrect array (constraint c3 not honored)
my_arr = '{'h1420568f, 'h5031fd43, 'h730cfe86, 'h105454e8, 'h4583b052, 'h1c167bce, 'h4b2f6faa, 'h7e643336, 'hd532e50b, 'h3fbca2b6, 'hd82f0210}

Any idea, why?

may the sum of my_arr is still 17 as 'hd532e50b and 'hd82f0210 are negative values
what’s the randomized value of inx_num?
the constraint of inx_num should be inside [0:my_arr.size()-1], right?
or else, let’s say if inx_num is randomized to 11 when my_arr.size == 11, the constraint c2 will never meet the pre-condition of (i==inx_num), as i is from 0 to 10.

You are casting each element in the sum from a 32-bit unsigned type to a 32-bit signed type. So some element may be treated as negative numbers and still have your sum be 17. It’s not a problem when the element size is 16 bits. All 16-bit values will be cast to a 32-bit signed number and still be considered positive values.

Also, for every 2N elements you want to add, you need N extra bits in the sum result to prevent overflow. For 16 elements, you need 4 extra bits.

constraint c3 {my_arr.sum() with(36'(item))== 17;}