Difficulty: Easy · Puzzle 4 of 6 · Topic: solve...before Bias Puzzles
Compute P(n==4) without solve-before, then the exact value with solve n before m.
class sb_mn;
rand int unsigned n;
rand int unsigned m;
constraint c {
n inside {[1:4]};
m inside {[0:3]};
m < n;
}
// Variant: solve n before m;
endclassCount legal m values for each n.
1) n=1 allows m={0} (1 tuple), n=2 allows {0,1} (2), n=3 allows {0,1,2} (3), n=4 allows {0,1,2,3} (4).
2) Total tuples = 1+2+3+4 = 10.
3) Baseline P(n==4)=4/10=0.4.
4) With solve n before m, n is chosen first, uniformly over the values that have a legal completion; all four values (1..4) do, so P(n==4)=1/4 exactly, then m is uniform over its legal values.
5) Legal solutions are unchanged.Answer: Without solve-before, P(n==4)=2/5 (40%); with `solve n before m`, P(n==4)=exactly 1/4 (n uniform over 1..4); the 10 legal tuples are unchanged.
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). n has 1,2,3,4 legal m values; solve n before m makes n uniform over 1..4. Verilator 5.048 legality only: every result satisfies m < n within the stated ranges.
Saying 'n is uniform because its domain is 1..4' is only true after solve n before m. Without it n is biased towards 4 by the m<n coupling (1:2:3:4 tuples). With it, the small-n cases (only 1 tuple) are over-represented relative to tuple counting.
Without solve-before, how would you still get n uniform? Model answer: randomize in two steps, `randomize(n) with { n inside {[1:4]}; }`, then `n.rand_mode(0)` and `randomize(m)` with m<n, but only on a fresh object. During the first call m is a state variable and `m < n` is still active, so a stale m biases n (after a round with m=3, n can only be 4 next time). Set `m = 0; n.rand_mode(1);` first, or simply use `solve n before m;`, which is LRM-defined and needs no state handling. (`n dist {1:=1,2:=1,3:=1,4:=1}` also works under the marginal reading, but is tool-dependent.)
Counting tuples, n equal to one allows one m, two allows two, three allows three and four allows four, ten tuples in total, so without solve-before the probability that n is four is four tenths. With solve n before m, n is chosen first, uniformly over one to four, so the probability that n is four is exactly one quarter, and m is then uniform below n. The trap is calling n uniform without the solve-before; the m less than n coupling biases it toward large values. Next I would check that the legal set is still ten tuples.