System verilog constraint such that sum of arr[10] is 100 without using .sum method

In reply to dve12:

by assuming each array element inside 1-20, we can write below logic

class sum;
   
   rand int list[10];

    constraint C {
         foreach(list[i]) {
	   ( ( i % 2) == 0 ) -> list[i] inside {[1:20]};
	   ( ( i % 2) == 1 ) -> list[i] == 20 - list[i-1];
	 }
    }
endclass

module main;
  initial begin
     sum i_sum = new();
     if ( !i_sum.randomize() ) $display("randomization failed");
     $display(" list = %p sum = %0d", i_sum.list, i_sum.list.sum());
  end
endmodule
1 Like