Help me understand this constraint:
constraint c_sum { array.sum() with (int’(item == 40)) > 1; }
My understanding: This line makes sure you have at least two items in your array with value == 40.
But when I tested this out this gives me exactly 2 item == 40 always .
what I wanted to do: To write a constraint for an array where my second maximum value is always == 40. I am able to achieve this int this way. I was able to generate random values for array with second max being 40. But I am always getting only 2 values to be equal to be 40.
If I change array.sum() with (int’(item == 40)) > 1; to >2; then I get 3 values, if I change to >3 then I get 4 values.
Help me understand the constraint.
/———————————-CODE————————————————————————————/
// I want to write a constriant where my second maximum value of an array is always equal to 40.
class ABC;
rand int array [10];
rand int max_value;
int count;
// making sure 40 is alteast is seen once
constraint c_sum { array.sum() with (int’(item == 40)) > 1; }
//making sure the max_vlaue is atleast seen once. makes 40 alsways second max
constraint c_max_value { array.sum() with (int’(item == max_value)) > 1; }
constraint c_max {max_value inside{[41:50]}; }
constraint c_0 {foreach (array[i]) array[i] inside {[0:50]}; }
function void post_randomize();
count = 0;
foreach (array[i])
begin
if(array[i] == 40)
count = count + 1; //. want to count how many values in my array after randomization are equal to 40
else if(array[i]>40)
array[i] = max_value;
end
endfunction
endclass
module tb;
initial begin
ABC abc = new;
repeat(50)
begin
abc.randomize();
$display (“array = %p”, abc.array);
abc.array.sort();
$display (“array_sort = %p | Count = %0d”,abc.array,abc.count);
$display (“--------------------------------------------------------------------------”);
end
end
endmodule
/—————————————————CODE—————————————————————/