Difficulty: Easy · Puzzle 3 of 6 · Topic: Constraint Solving Order Puzzles
Compute P(val==2) and P(mode==1) by counting legal tuples. Why is val not uniform over {1,2,3}?
class overlap_if;
rand bit mode;
rand int unsigned val;
constraint c {
val inside {[1:3]};
if (mode) val inside {1,2};
else val inside {2,3};
}
endclassList legal (mode,val) pairs explicitly.
1) mode=1 allows val {1,2} -> 2 tuples.
2) mode=0 allows val {2,3} -> 2 tuples.
3) Total legal tuples=4: (1,1),(1,2),(0,2),(0,3).
4) val==2 appears in two tuples -> P(val==2)=2/4=1/2.
5) mode==1 appears in two tuples -> P(mode==1)=1/2.Answer: P(val==2)=1/2 and P(mode==1)=1/2; overlap on value 2 creates bias versus a naive 1/3-per-value assumption (val==1 and val==3 are 1/4 each).
Checked by running a model of the rule in a Python model. Exact enumeration of the constraint set per the LRM (the LRM defines the distribution; simulator solvers need not be uniform). Value 2 is reachable from both branches, so it owns two of the four (mode,val) tuples; solve val before mode makes val uniform.
The value 2 is reachable from both branches, so it owns two of the four tuples. Reasoning on the val domain {1,2,3} alone hides that multiplicity; the legal set lives in (mode,val) space.
What small constraint would make val exactly uniform over 1,2,3? Model answer: add `solve val before mode;`. The legal set is unchanged (4 tuples), val is chosen first uniformly over its 3 feasible values (1/3 each), and mode is then 50/50 only when val==2 (forced to 1 for val==1, forced to 0 for val==3). P(mode==1) stays 1/2.
I would list the legal pairs of mode and val: mode one gives val one or two, mode zero gives val two or three, so four pairs. Val equal to two appears in two of the four, so its probability is one half, and mode one is also one half. The trap is assuming val is uniform over one, two, three; the overlap on value two gives it double weight, and one and three get a quarter each. Next I would add solve val before mode if I wanted val uniform.