In reply to sharvil111:
When you say you want to toggle a bit, that is not the same as setting a bit. A toggle implies a change in state from one transaction to the next. The following constraint will, on average, toggle 3.2 bits out of 32 bits on each call to randomize().
constraint toggle_bits {$countones(data ^ const'(data)) dist {3:= 80, 4:=20};}
This reads “the count of ones when you XOR the (rand data) with the (previous data) should be 3 or 4”.
But if you want to spread the 10% toggles over N transactions, that is a much harder problem. Assume the case where N=100 transactions. Then you have the potential for (100-1)*32 = 3168 bit toggles, so 10% of that is 316.8 toggle. Now the question becomes, is it OK to use the above constraint (assuming you adjust the average to 3.168 bits toggled per transaction), or do you need a wider distribution of bits toggled per transaction (i.e. sometimes no bits, sometimes all bits toggling per transaction)? If so, there is no way to do that with independent randomizations.
What you can do is randomize N transaction in an array and constrain the toggles within the array.
class data_c;
rand bit [31:0] data[];
int P=10; // Percentage
rand int unsigned N;
constraint data_size {
N inside {[1:10]};
data.size() == N;
}
constraint toggles {
data.sum() with ( $countones((item.index<N-1)*(item^data[item.index+1])) ) == (N-1)*32/P ;
}
endclass