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: rand, randc & randomize()

Part 3 · Constraint Randomization · Intermediate

Q&A: rand, randc & randomize()

Revision: what rand and randc guarantee, the return value, and what one solve touches.

On this page

    Why this matters

    A candidate said randomize() returns 0 on failure and stopped there. The follow-up, what the fields hold afterwards, decided the interview.

    After this lesson you can: state precisely what rand, randc and randomize() guarantee, and what one call touches.

    Note

    Revision page. Answer each question before opening it; the full lessons are rand vs randc, The randomize() Call, Hooks and Nested Objects.

    Predict first What does randc bit [1:0] c promise that rand bit [1:0] r does not?

    Every value once per cycle of four calls, with no repeat inside a cycle. rand draws independently each call, so repeats are normal.

    Predict first randomize() returns 0. What do the rand fields hold, and what must the caller do?

    Their previous values: a failed call assigns nothing. The caller checks the return value and stops, or every later transaction re-sends stale stimulus.

    Predict first A rand handle inside the object is null. What does randomize() do with it?

    The standard skips it and still returns 1; the solver never builds objects. Verilator 5.048 skipped it only when the handle's class had no constraints of its own, and otherwise stopped with a null-pointer error. Build every handle in new().

    What rand and randc each guarantee

    rand

    randc

    Repeats across calls

    Allowed

    None until the cycle ends

    State

    None

    Per field, per object

    Mixes with dist

    Yes

    No: weighting contradicts cycling

    Widths

    Any

    Tools may cap it; the cap is at least 8 bits

    The return value and one solve

    randomize() returns 1 when the solver found a solution and 0 when the set was empty. One call solves every rand field of the object and its parents whose mode is on, plus every non-null rand child, in one problem; plain fields are read as constants.

    The bench checks each answer above on Verilator: 100 calls of the rand and randc pair, a null child then a built one, and a call that cannot succeed.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class demo;
      rand  bit [1:0] r;                            // any of 0..3 each call: repeats allowed
      randc bit [1:0] c;                            // a permutation of 0..3, then a new one
    endclass
    
    class header;                                  // no constraints of its own: Verilator stops on a null handle whose class has some
      rand bit [3:0] ver;
    endclass
    
    class packet;
      rand bit [7:0] payload_len;
      bit      [7:0] max_len = 40;                  // state: read by the solver, never written
      rand header    hdr;                           // nested: solved only if it exists
      constraint c { payload_len <= max_len; }
    endclass
    
    Terminal
    100 calls: rand repeated its previous value 23 times; randc never repeated inside a cycle
    hdr null: randomize() returned 1, hdr is still null
    hdr built: ver 3, payload_len 9 (max_len 40)
    %Warning-UNSATCONSTR: /var/folders/41/dgs80y1569b4x8pwflv_w9jr0000gn/T/rtbtb-K1WcxI/t63.sv:14: Unsatisfied constraint: 'constraint c { payload_len <= max_len; }'
    %Warning-UNSATCONSTR: /var/folders/41/dgs80y1569b4x8pwflv_w9jr0000gn/T/rtbtb-K1WcxI/t63.sv:45: Unsatisfied constraint: 'ok = p.randomize() with { payload_len > 200; };'
    payload_len > 200 against max_len 40: returned 0, payload_len still 9
    PASS: randc cycled, the null handle was skipped and then solved once built, and the failed call kept stale values

    Common pitfall

    Discarding the return value is the costliest habit on this page. The failure mode is silence: plausible coverage over stimulus that never ran.

    Interview

    Junior answers stop at 1 and 0. Senior answers add stale values on failure, name the silent-stimulus bug class, and say the bench stops. Volunteer the follow-up before it is asked.

    Check yourself When does randomize() legitimately return 0?

    On a contradiction: class rules against an inline with, parent against child, state bounds squeezed empty, or a wrap with no solution. Each is a design signal, not noise.

    Remember

    • randc: every value once per cycle; rand: independent draws.

    • A failed call assigns nothing.

    • Null rand handles are never built by the solver.

    Rule of thumb: check every return value, and build every handle in new().

    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.

    Verilator skipped a null rand handle only when its class had no constraints; otherwise the run stopped with a null-pointer error.

    Related topics

    • Q&A: dist, inside, soft, solve-before

    Previous

    97 / 259

    Next: Q&A: dist, inside, soft, solve-before

    Up next

    Up nextQ&A: dist, inside, soft, solve-before

    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