Difficulty: Easy · Puzzle 2 of 6 · Topic: Distribution Puzzles (`dist`, `:=` vs `:/`)
Compute P(x==0) and P(x==7).
class dist_mix1;
rand int unsigned x;
constraint c {
x dist { [0:1] := 4, [2:9] :/ 4 };
}
endclassThe second range has 8 values, so :/ splits the total weight across 8.
1) Values 0,1 each get weight 4 from :=.
2) Values 2..9 each get weight 4/8=0.5 from :/.
3) Total weight = 2*4 + 8*0.5 = 12.
4) P(x==0)=4/12=1/3.
5) P(x==7)=0.5/12=1/24.Answer: P(x==0)=1/3 (~33.33%) and P(x==7)=1/24 (~4.17%).
Checked by running a model of the rule in a Python model and Verilator 5. Exact enumeration of the constraint set per the LRM (the LRM defines the distribution; simulator solvers need not be uniform). Per-value weights: 4 for each of 0,1 and 4/8 for each of 2..9, total 12. Verilator 5.048 legality only: the dist solves and x stays in 0..9; no frequency is asserted.
Both clauses say '4', but the first is 4 per value (2 values -> 8 total) and the second is 4 for the whole range (8 values -> 0.5 each). The :/ range as a whole gets only 4/12 = 1/3 of the probability while holding 8 of the 10 values.
If [2:9] changes to := 4, what does P(x==7) become? Model answer: all ten values now carry weight 4, total 40, so P(x==7)=4/40=1/10 and the distribution is uniform.
Values zero and one get weight four each from :=, and the range two to nine gets four in total from :/, so each of those eight values gets a half. The total weight is twelve, so the probability that x is zero is a third and that x is seven is one twenty-fourth. The trap is that both clauses say four, but one is four per value and the other is four for the whole range. Next I would switch the second range to := if I wanted x uniform.