Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Start Here6
  • SystemVerilog Language Foundations28
  • Object-Oriented SystemVerilog for Verification30
  • Constraint Randomization — Interview-Critical Deep Dive36
    • Randomization Basics: rand, randc & randomize()
    • Constraint Operators & Expressions
    • Dynamic Constraint Control
    • Randomizing Arrays and Queues
    • How the Constraint Solver Works
    • Debugging Randomization Failures
    • Interview Drills — Constraints and Randomization Q&A
      • Even/Odd, Alignment & Divisibility
      • Powers of 2, One-Hot & Bit Games
      • Unique Values & Ordering
      • Sum, Checksum & Distribution Drills
      • Dependent-Field Puzzles
      • Q&A: rand, randc & randomize()
      • Q&A: dist, inside, soft, solve-before
      • Q&A: Controlling Randomization
      • Q&A: Arrays & Nested Objects
  • SystemVerilog Assertions — Interview-Critical Deep Dive40
  • Functional Coverage in SystemVerilog22
  • Testbench Architecture in SystemVerilog23
  • Advanced Language & Integration25
  • Senior & Interview Preparation21
  • Principal-Level Verification28
Loading lesson

‹Interview Drills — Constraints and Randomization Q&A
  1. Topics
  2. SystemVerilog
  3. Constraint Randomization — Interview-Critical Deep Dive
  4. Interview Drills — Constraints and Randomization Q&A
  5. Q&A: Controlling Randomization

Part 3 · Constraint Randomization · Intermediate

Q&A: Controlling Randomization

Revision: freezing a field, relaxing a rule, subclass overrides, and knob-rate injection.

On this page

    Why this matters

    An earlier version of this page modelled an enabled constraint as clamping values and a frozen field as zero. Both pictures were wrong, and the bench now shows the real behaviour.

    After this lesson you can: freeze a field, relax a rule, override a block in a subclass, and drive an error rate from a knob.

    Note

    Revision page. Answer each question before opening it; the full lessons are rand_mode(), constraint_mode(), Constraint Override via Inheritance, State Variables and Cross-Object.

    Predict first How do you freeze addr at 8000 while the rest of the object keeps randomizing?

    t.addr.rand_mode(0); t.addr = 16'h8000;. The field becomes a constant to the solver, and every other field still solves around it.

    Predict first The class caps len at 64. A test calls randomize() with { len > 64; }. What happens, and what is the fix?

    The call returns 0, because the inline rule is ANDed with the cap. The fix is c_len.constraint_mode(0) for that test, or a subclass that replaces c_len.

    Predict first A subclass declares c_len again with a new range, and c_hi as a new block. What does each do?

    The same name replaces the parent's c_len. The new name adds c_hi to every inherited rule.

    Mode switches, inline rules and subclasses

    Knob

    Switches

    Lasts

    rand_mode(0) plus an assignment

    One field

    Until rand_mode(1)

    constraint_mode(0)

    One named block

    Until constraint_mode(1)

    Inline with

    One call

    That call; ANDed with every class rule

    Subclass block

    One block by name

    Every object of the subclass

    Knobs and injection rates

    A knob is a plain field the test sets before the call. A dist whose weights read the knob, plus a pair of implications that turn the decision into a stimulus change, gives rate-controlled error injection. Keep the knob in range in the test, before the call.

    The bench runs every answer above on Verilator: a frozen field over 100 calls, the inline contradiction and its fix, a subclass override, and error injection at 0, 25 and 100 percent.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class txn;
      rand bit [15:0] addr;
      rand bit [15:0] len;
      constraint c_len { len inside {[1:64]}; }
      constraint c_fit { int'(addr) + int'(len) <= 65536; }
    endclass
    
    class sub_txn extends txn;
      constraint c_len { len inside {[1:16]}; }                    // same name: replaces the base rule
      constraint c_hi  { addr inside {[16'h8000:16'hFFFF]}; }      // new name: adds to the base rules
    endclass
    
    class err_txn extends txn;
      int unsigned err_pct;                                        // state knob, set by the test
      rand bit inject_err;
      constraint c_len  { len inside {[1:255]}; }                  // replaced, so len > 64 is reachable
      constraint c_rate { inject_err dist { 1 := err_pct, 0 := 100 - err_pct }; }
      constraint c_arm  { inject_err -> len > 64; !inject_err -> len inside {[1:64]}; }
    endclass
    
    Terminal
    freeze: addr stayed 8000 for 100 calls while len kept changing (last 63)
    %Warning-UNSATCONSTR: /var/folders/41/dgs80y1569b4x8pwflv_w9jr0000gn/T/rtbtb-K1WcxI/t65.sv:4: Unsatisfied constraint: 'constraint c_len { len inside {[1:64]}; }'
    %Warning-UNSATCONSTR: /var/folders/41/dgs80y1569b4x8pwflv_w9jr0000gn/T/rtbtb-K1WcxI/t65.sv:38: Unsatisfied constraint: 'ok = t.randomize() with { len > 64; };'
    inline len > 64 against the class cap: randomize() returned 0
    with c_len switched off: returned 1, len 32767
    subclass: len stayed 1 to 16 and addr 8000 or above over 100 calls
    err_pct 0: 0 errors in 400 packets
    err_pct 25: 99 errors in 400 packets
    err_pct 100: 400 errors in 400 packets
    PASS: freeze, relax, subclass override and knob-rate injection all did what the answers say

    This bench replaces an earlier model on this page that clamped values and forced frozen fields to 0. Both pictures were wrong: an enabled constraint rejects solutions instead of clamping them, and a frozen field keeps its own value. The output above shows the real behaviour.

    Common pitfall

    An inline with that contradicts the class returns 0 and keeps stale values. Check the return value at every call site, without exception.

    Interview

    Expect the freeze drill: park one field, relax one rule, and inject errors at a knob rate. Each answer names the exact call: rand_mode, constraint_mode, and a state knob read by a dist.

    Check yourself Where do constraints, knobs and seeds each belong?

    Constraints in the transaction, knobs in the sequence or config, seeds and plusargs in the test. Each override lives at the level that owns it.

    Remember

    • rand_mode(0) plus an assignment parks a field.

    • An inline with is ANDed with the class; a contradiction returns 0.

    • Same block name replaces; a new name adds.

    Rule of thumb: relax a rule by name, never by deleting it from the class.

    How this was checked

    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.

    Error injection at 0, 25 and 100 percent gave 0, 99 and 400 errors in 400 packets.

    Related topics

    • Q&A: Arrays and Nested Objects

    Previous

    99 / 259

    Next: Q&A: Arrays & Nested Objects

    Up next

    Up nextQ&A: Arrays & Nested Objects

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Even/Odd, Alignment & Divisibility
    • Powers of 2, One-Hot & Bit Games
    • Unique Values & Ordering