Drill 1. rand bit [3:0] v[8]: all eight values are different.
Drill 2. rand bit [7:0] a[]: 5 to 8 values, strictly ascending.
Drill 3. rand bit [7:0] a[6]: exactly one element is bigger than all the others. A helper field rand bit [2:0] top is there if you want it.
Write the three answers before opening the reveal. Copy the starter into the Code Lab, fill in each empty answer_c, and run it on Verilator. The grader solves each class hundreds of times and checks every result, then pins a few values that must be legal; it prints ok or the first rule your answer broke.
// Drill starter: write each answer_c, then run it.
// Drills: unique values and ordering.
class drill1; // 8 different values, each 0 to 15
rand bit [3:0] v[8];
constraint answer_c { /* your answer here */ }
endclass
class drill2; // 5 to 8 values, strictly ascending
rand bit [7:0] a[];
constraint answer_c { /* your answer here */ }
endclass
class drill3; // exactly one element is bigger than all the others
rand bit [7:0] a[6];
rand bit [2:0] top; // a helper index: use it or not
constraint answer_c { /* your answer here */ }
endclass
randc. Does it help?No. randc stops a value repeating across successive calls of one field. It says nothing about eight elements within one call, and it cannot be combined with rand on the same field.
One set of answers, run through the same grader:
// Drills: unique values and ordering.
class drill1; // 8 different values, each 0 to 15
rand bit [3:0] v[8];
constraint answer_c { foreach (v[i]) foreach (v[j]) if (i < j) v[i] != v[j]; }
endclass
class drill2; // 5 to 8 values, strictly ascending
rand bit [7:0] a[];
constraint answer_c { a.size() inside {[5:8]}; foreach (a[i]) if (i > 0) a[i] > a[i-1]; }
endclass
class drill3; // exactly one element is bigger than all the others
rand bit [7:0] a[6];
rand bit [2:0] top; // a helper index: use it or not
constraint answer_c { top < 6; foreach (a[i]) if (i != top) a[i] < a[top]; }
endclass
drill 1, all different: ok
drill 2, strictly ascending: ok
drill 3, unique maximum: ok
PASS: all 3 drills rightAnswer | Spelling | Verdict |
|---|---|---|
|
| The standard's one keyword; see the tool note below |
Pairwise |
| Portable: 28 relations for 8 elements |
| Cycling across calls | Wrong tool: says nothing within one solve |
The reveal uses the pairwise form on purpose. Verilator 5.048 ignored unique { v } here: with no other rule naming the elements, 85 of 100 results repeated a value. With a range rule beside it, it worked. The pairwise form works on every tool, so it is the safe interview answer, and saying why is a strong follow-up.
Ordering needs only neighbour relations, because they chain: a[0] < a[1] < ... < a[n-1]. Strict ascent therefore gives uniqueness for free. The low-side guard if (i > 0) keeps the loop inside the array.
The unique maximum takes a random index for the position, and every other element stays strictly below a[top]. Pinning the maximum to a fixed slot is right only when the spec fixes the slot, and the grader pins maxima at index 0 and index 5 to catch it.
Run with Verilator 5.048 (--binary --timing) on 2026-10-18; every bench printed the PASS line shown. Verilator prints a harmless WIDTHTRUNC warning for if (!obj.randomize()), because randomize() returns an int, and an UNSATCONSTR warning when a solve fails on purpose. Icarus 13 does not support constraints.
Each starter was run with its answers left empty, and its grader reported all three drills wrong; each revealed answer passed the same grader. The graders also caught an unwidened size * count and an over-tight even rule when tried.
unique { v } on a fixed array with no other element rule repeated a value in 85 of 100 results; with a range rule beside it, none did.