Hardware interview practice
SystemVerilog Soft Constraints Explained
What happens when randomize is called?
class C;
rand int x;
constraint defaults { soft x inside {[0:9]}; }
endclass
C c = new;
initial assert(c.randomize() with { x == 20; });Answer choices
- A. Randomization fails because 20 is outside 0 through 9
- B. The inline constraint is ignored
- C. Randomization succeeds with x clamped to 9
- D. Randomization succeeds with x equal to 20
Concise answer
The answer and the key reason
Deeper explanation
Work through the implementation and tradeoffs
A soft constraint supplies a default that remains active only when it can coexist with all applicable hard constraints. Here the inline `with` clause requires exactly 20, while the class-level preference would allow only 0 through 9. Because those sets do not overlap, the soft expression yields and the hard equality determines the value.
If `soft` were removed, both expressions would be mandatory and there would be no legal assignment, causing `randomize()` to return zero. Nothing in constrained randomization automatically clips 20 to 9, and the inline clause is not ignored. Soft constraints are therefore useful for reusable defaults that individual tests may intentionally replace.
What to remember
- Soft means overridable default
- Inline equality remains hard
- No automatic value clamping
Practice the complete prompt
