Consider the below code :
class packet;
rand bit addr;
constraint addr_range { addr dist {1 := 60, 0 :=40 }; }
endclass
module constr_dist;
initial begin
packet pkt;
pkt = new();
$display("------------------------------------");
repeat(10) begin
pkt.randomize();
$display("\taddr = %0d",pkt.addr);
end
$display("------------------------------------");
end
endmodule
When i do randomize with dist constraint i see that distribution of 1 and 0 is not exactly 60% - 40% everytime. It gives me a distribution with a variance. Is it possible to somehow get the exact distribution? What probability distribution does system verilog use while randomizing with dist constraints?
Hi Dave,
Even with 2000 repeats I am still getting a variance of about 5%. I guess there is no way to get an exact distribution through dist constraint.
A distribution is a statistical probability of value occuring in a range. Each time you call randomize, the previous results have no effect on the new result, the probabilities remain the same.
Take a simpler example where the dist probability is 1 := 50, 0 :=50
. If you call randomize twice, there are 4 possible outcomes with equal probability: 0-0, 0-1, 1-0, and 1-1. Only 2 out of 4 outcomes gives you an exact 0-1or 1-0 distribution, or 50% chance of seeing an exact distribution.
If you increase the random repetition to 4 times, then there are 16 possible outcomes with only 6 of them giving you an exact distribution of 2 0’s and 2 1’s. or 37.5%. 2000 repetitions, the probability goes down to 2.5%. The probability keeps getting lower as you increase the repetition count, but at the same time, the average distribution keeps getting closer to 50/50.
Thank you Dave! That makes sense.