Constraint with array reduction method

class Pattern;
rand bit pat[10];
constraint sum_c { pat.sum() == 4’h6;}
endclass

After randomization, pat always contain items with 0 value. How do I randomize single bit array with pat.sum == 6?

In reply to superUVM:

You are not checking the result from the non-void method
randomize()
(You should be getting a warning).

If you had checked the result, you would see that the randomization failed, leaving pat unmodified. There reason is the result of the sum() method has the same type as the array element, 1 bit. You need a cast.

constraint sum_c { pat.sum(Bt) with (4'(Bt)) == 4'h6;} 

This is explained in section 7.12.3 Array reduction methods of the 1800-2017 LRM.