Write a constraint to generate a random value such that it always has 10 bits as 1 for a 32-bit variable. without using the $countones built-in function

class constraint_ex;
 rand bit [31:0] data;
 
function int count_ones(bit [31:0] data_in);
  count_ones=0;
  //$display("Data:%0b",data_in);
  while(data_in !=0) begin
        count_ones +=data_in&1'b1;
        data_in = data_in>>1;
  end
  //$display("Count:%0d",count_ones);
endfunction

//constraint c_ones {$countones(data)==10;}
constraint c_ones {count_ones(data)==10;}
endclass
 
module top;
  constraint_ex cc=new();
  initial begin
    for (int i=0;i<8;i++) begin
    assert(cc.randomize())
    $display("CONSTRAINT : Value of DATA:%0d,%0b",cc.data,cc.data);
  end
end
endmodule

I am facing constraint error with above code.
But the code is working when I use data size as 8bits, 16bits, 24bits, 30bits.

Can some one explain about this issue

In reply to rjadepu1234:

I do not believe you actually ran this code. If data size is 8 bits, how could the count of ones be 10?

See Constraint | Verification Academy

In reply to dave_59:

Hi Dave,
I apologize for forgetting to mention the use of 8 bits earlier. In fact, I did experiment with 8 bits by adjusting the count to 5.

The 15-bit, 24-bit, and 30-bit components worked without any issues.
Below is output:
for 16bits:
xcelium> run
CONSTRAINT : Value of DATA:40251,1001110100111011
CONSTRAINT : Value of DATA:62898,1111010110110010
CONSTRAINT : Value of DATA:14183,11011101100111
CONSTRAINT : Value of DATA:22259,101011011110011
CONSTRAINT : Value of DATA:56859,1101111000011011
CONSTRAINT : Value of DATA:58973,1110011001011101
CONSTRAINT : Value of DATA:48782,1011111010001110
CONSTRAINT : Value of DATA:60814,1110110110001110

for 24bits:
xcelium> run
CONSTRAINT : Value of DATA:14185664,110110000111010011000000
CONSTRAINT : Value of DATA:1424513,101011011110010000001
CONSTRAINT : Value of DATA:772126,10111100100000011110
CONSTRAINT : Value of DATA:4357768,10000100111111010001000
CONSTRAINT : Value of DATA:5865782,10110011000000100110110
CONSTRAINT : Value of DATA:10021388,100110001110101000001100
CONSTRAINT : Value of DATA:2455813,1001010111100100000101
CONSTRAINT : Value of DATA:1189550,100100010011010101110

for 31bits:
xcelium> run
CONSTRAINT : Value of DATA:1347651841,1010000010100111000100100000001
CONSTRAINT : Value of DATA:1888489592,1110000100100000001000001111000
CONSTRAINT : Value of DATA:119652876,111001000011100001000001100
CONSTRAINT : Value of DATA:1208828300,1001000000011010100000110001100
CONSTRAINT : Value of DATA:371540080,10110001001010100000001110000
CONSTRAINT : Value of DATA:1476609348,1011000000000110100010101000100
CONSTRAINT : Value of DATA:1308690522,1001110000000010000100001011010
CONSTRAINT : Value of DATA:286336298,10001000100010010010100101010

Below error i am getting for 32bits:
xcelium> run
assert(cc.randomize())
|
xmsim: *W,SVRNDF (./testbench.sv,24|22): The randomize method call failed. The unique id of the failed randomize call is 0.
Observed simulation time : 0 FS + 0
xmsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:

constraint c_ones {count_ones(data)==10;} (./testbench.sv,17)

xmsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:

Var_Name Type Status Current_Value Source

data (U32) SOLVED EARLY 726800096 (0x2b5216e0) ./testbench.sv ; line 4
count_ones( .data_in( data ) ) (S32) SOLVED EARLY 13 (0xd) ./testbench.sv ; line 17

My question here is
why assertion failure is coming only when it is 32 bits?
How come the same code is working for 30bits, 24bits, 16bits?

Thanks
Rajesh A

In reply to rjadepu1234:

Try running 30bits for 100 repetitions. It does not always succeed. Look at the link I provided above about using functions in constraints.