Sum of Array Element And its Size < a defined value and the outputs are totally random

Hello !

Trying a basic array to get with a defined sum value but the results are totally random.

a. Just array.sum == 1000 won’t generate the required random variables ?
b. If not for using the other constraint [arr.sum with (item<(1000/arr.size)) == 1000;], the tool errors out with below error [not sure why its considering the 1’].

constraint arr_sum_c    // (from this) (constraint_mode = ON) (rand_array_with_sum.sv:5)
{
   ((1'(((((((((((arr[0] < (1000 / 10)) + (arr[1] < (1000 / 10))) + (arr[2] < (1000 / 10))) + (arr[3] < (1000 / 10))) + (arr[4] < (1000 / 10))) + (arr[5] < (1000 / 10))) + (arr[6] < (1000 / 10))) + (arr[7] < (1000 / 10))) + (arr[8] < (1000 / 10))) + (arr[9] < (1000 / 10))))) == 1000);
}

module rand_array_with_sum;
    class rand_array_with_sum;
      rand int unsigned arr[10];

      constraint arr_sum_c {
        // arr.sum with (item<(1000/arr.size)) == 1000; 
        arr.sum == 1000; 
      } 

      function print_info();
       foreach (arr[s]) $display("Ele %0d  Val %d", s, arr[s]); 
      endfunction: print_info
    
    endclass: rand_array_with_sum


    initial begin
        rand_array_with_sum raws = new();
        assert (raws.randomize());
        raws.print_info();
    end

endmodule: rand_array_with_sum

Please provide your comments !

In reply to desperadorocks:

It’s not clear what your question is. What results are you expecting to see? What results are you seeing? Using unsigned ints will result in a large range of values since they can add to a sum less than 1000. Do you want to use signed numbers?

Remember that addition of unsigned ints is modulo 2**32, so that the sum of large values can ultimately equal 1000. The constraint you are probably looking for is


constraint arr_sum_c
{
   arr.sum == 1000;
   foreach (arr[s]) arr[s] <= 1000;
}

In reply to cgales:

Sorry missed that part of modulu calculation. Got it going.

In reply to sbellock:

Yup, got it going. Appreciate your response !