Difficulty: Medium · Puzzle 4 of 6 · Topic: Constraint Solving Order Puzzles
randomize() is called six times in a row on the same object. What is guaranteed about the six printed values, and what is the exact probability that call 4 returns the same value as call 3? Assume each randc cycle is a uniformly random permutation of the legal values.
class rc_cycle;
randc bit [1:0] r;
constraint c_skip { r != 2; }
endclass
rc_cycle o = new();
repeat (6) begin
assert(o.randomize());
$write("%0d ", o.r);
endThe cycle length is the number of legal values, not 2**width.
1) The legal values of r are {0,1,3}, so the randc cycle has length 3, not 4.
2) Calls 1-3 are one full cycle: each of 0, 1, 3 exactly once, in some order. Calls 4-6 are a second, independent cycle with the same property.
3) Guaranteed: the value 2 never appears, calls within a cycle never repeat, and over the six calls each of 0, 1, 3 appears exactly twice. The order inside a cycle is not guaranteed.
4) A repeat can only happen at the cycle boundary: call 4 (first of cycle 2) equals call 3 (last of cycle 1). With two independent uniform permutations of 3 values there are 6*6=36 pairs, and 12 of them have last(cycle1)==first(cycle2), so P=12/36=1/3.
5) By the same symmetry, P(call 1 == 3)=1/3.Answer: Cycle length 3: the six values are two permutations of {0,1,3} (each value exactly twice, never 2). P(call 4 == call 3) = 1/3 exactly.
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). Models each randc cycle as a uniformly random permutation of the legal values {0,1,3}, as the prompt assumes. Compile + run on Verilator: 50 fresh objects, six randomize() calls each; the value 2 never appears and no value repeats inside a three-call cycle (the order inside a cycle is not asserted).
A common answer is 'r cycles through 0,1,2,3', i.e. a cycle of 4. A hard constraint removes value 2 from the cycle itself, so the length is 3 and 2 never shows. Also 'no back-to-back repeats' is false across the cycle boundary (1/3 chance); only the per-cycle no-repeat is guaranteed. The order inside a cycle is implementation-defined (Verilator 5.048 produced cycles 3 1 0, 3 0 1, 0 3 1, all legal).
What happens to the cycle if the constraint becomes `r inside {1,3};`? Model answer: only two values are legal, so the cycle length is 2 and four consecutive calls give each of 1 and 3 exactly twice; then P(call 3 == call 2)=1/2 under the uniform-permutation assumption (the two cycles are independent, orders 1,3 or 3,1). If constraints change mid-run the legal set is always respected, but where in the cycle you resume is implementation-defined.
The legal values are zero, one and three, so the randc cycle has length three, not four, and two never appears. Calls one to three are each value once in some order, and calls four to six are a second cycle, so each value shows up exactly twice. A repeat can only happen at the cycle boundary, and the chance that call four equals call three is exactly one third. The trap is expecting a cycle of four or believing there are never back-to-back repeats. Next I would check what happens if the constraint changes mid-run, since where the cycle resumes is implementation defined.