Constraining Array.sum( ) for One/Two Dimesional Unpacked Array

In reply to Have_A_Doubt:

your requirement is simple.
try to simplify step by step, no need to solve everything in constraint body.


class trial;
    rand bit [1:0] tmp[];
    rand int first_dim;
    rand int second_dim;
    rand int total;
    bit [1:0] arr[][];      // final output
    constraint c {
        first_dim == 9;     // your design
        second_dim == 2;    // your design
        total == 27;        // your design
        tmp.size() == first_dim * second_dim;
        tmp.sum() with (int'(item)) == total;
    }
    function void post_randomize(); 
        arr = new[first_dim];
        foreach (arr[i]) arr[i] = new[second_dim]; 
        for (int i = 0; i < first_dim; i++) begin
            for (int j = 0; j < second_dim; j++) begin
                arr[i][j] = tmp[i*second_dim+j];
            end
        end
        tmp.delete();
    endfunction
endclass