Difficulty: Medium · Puzzle 5 of 6 · Topic: Distribution Puzzles (`dist`, `:=` vs `:/`)
Use the usual 'dist is the marginal' reading: the dist on is_err sets P(is_err), and the dist on code only shapes code within the branch. Compute P(code==0) and P(code==501).
class err_code;
rand bit is_err;
rand int unsigned code;
constraint c_prob { is_err dist {0 := 9, 1 := 1}; }
constraint c_map {
if (is_err) code dist { [500:503] :/ 4 };
else code == 0;
}
endclassUse conditional probability: P(code=v)=P(branch)*P(v|branch).
1) Reading used: the dist on is_err is its marginal, so P(is_err=1)=1/10 and P(is_err=0)=9/10.
2) If is_err=0, code is deterministically 0.
3) If is_err=1, [500:503] :/ 4 makes each of 500..503 equal at 1/4 (weight 1 each).
4) Therefore P(code=0)=9/10.
5) P(code=501)= (1/10)*(1/4)=1/40.Answer: Under the marginal reading: P(code==0)=90% and each error code 500..503 is 2.5% (1/40), including 501.
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). Computes both stated readings (is_err dist as the marginal vs. weights multiplied over joint tuples); the LRM does not pin this down, so the page states the reading. Verilator 5.048 legality only: is_err=0 always pairs with code 0 and is_err=1 with 500..503; no probability is asserted.
The 9/10 and 1/40 answer rests on that stated reading, the reading assumed here; the LRM does not spell out joint semantics, and tools may differ. If you instead multiply the weights over the joint tuples, the legal tuples are (0,0) with weight 9 and (1,500..503) with weight 1 x 1 = 1 each, total 13, giving P(code==0)=9/13 and P(code==501)=1/13. Verilator 5.048 measured P(code==0)~0.90, consistent with the marginal reading. Always state which reading you assume.
How would you make all five outcomes {0,500,501,502,503} exactly uniform? Model answer: weight the branch by the number of outcomes in it: `is_err dist {0 := 1, 1 := 4}`. Then P(is_err=1)=4/5, each error code is 4/5 * 1/4 = 1/5, and code==0 is 1/5. If you instead declare the joint-weight reading, the tuples are (0,0) and four error tuples, so equal weights `{0 := 1, 1 := 1}` already give 1/5 each; the right answer depends on the reading you state.
I would state my reading first: the dist on is_err is its marginal, so error has probability one tenth. When there is no error, code is forced to zero, so the probability that code is zero is nine tenths, and each error code from 500 to 503 is one fortieth, including 501. The trap is that the LRM does not spell out the joint semantics, so a tool that multiplies the weights over tuples would give nine thirteenths and one thirteenth instead. Next I would check what my target tool actually does, or weight the branch to avoid the ambiguity.