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

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
    • Constraint Solving Order Puzzles
    • Distribution Puzzles (`dist`, `:=` vs `:/`)
    • solve...before Bias Puzzles
    • randomize() with Puzzles
      • Inline Implication That Looks Like a Conflict
      • Constraint Override by Name in a Derived Class
      • unique and Array Element Counting
      • constraint_mode Changes Pass/Fail
      • rand_mode(0) Freezes Value and Can Overconstrain
      • pre_randomize Assignment vs Solver and post_randomize Call Count
    • Randomization & Constraint Puzzles: Tricky Q&A
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←randomize() with Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Randomization & Constraint Puzzles
  4. randomize() with Puzzles
  5. rand_mode(0) Freezes Value and Can Overconstrain
On this page

    VLSI DV Interview PuzzlesAll levels

    rand_mode(0) Freezes Value and Can Overconstrain

    Will randomize succeed? Explain the exact reason.

    Puzzle

    Difficulty: Medium · Puzzle 5 of 6 · Topic: randomize() with Puzzles

    Will randomize succeed? Explain the exact reason.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    class rw_req;
      rand bit is_wr;
      rand bit [3:0] len;
      constraint c {
        if (is_wr) len inside {[1:4]};
        else       len inside {[8:12]};
      }
    endclass
    
    rw_req r = new();
    r.len = 10;
    r.len.rand_mode(0);
    assert(r.randomize() with { is_wr == 1; });

    Hint

    len is no longer randomized but still participates in active constraints.

    Step-by-step solution

    Diagram
    1) rand_mode(0) on len freezes len at current value 10.
    2) Inline sets is_wr=1, activating len inside 1..4.
    3) Frozen len=10 violates active constraint.
    4) No legal assignment exists, so randomize fails and len stays 10 (Verilator 5.048: returns 0, len==10).

    Answer

    Answer: It fails 100%: len is fixed to 10 and cannot satisfy the is_wr==1 branch constraint len in [1:4].

    How this was checked

    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). A frozen rand_mode(0) variable is a single fixed value that every active constraint still has to accept. Verilator 5.048: with len frozen at 10 the is_wr==1 call returns 0 and len stays 10; is_wr==0 (and no inline) pass with is_wr=0.

    Why candidates get it wrong

    rand_mode(0) turns len into a state variable, but the constraints that mention it stay active and are checked against its frozen value 10. It is not 'excluded from constraints'. That is why this fails, while the same call with `is_wr == 0` would pass (10 is in 8..12) with exactly one solution.

    Interviewer follow-up

    How can you preserve len=10 and still make randomize pass? Model answer: let the solver choose the branch that fits: `with { is_wr == 0; }` (len=10 lies in 8..12; is_wr=0 is the only solution), or drop the inline and let is_wr float (the solver then must pick is_wr=0). To keep is_wr==1 instead you must change len, e.g. `r.len.rand_mode(1)` or assign a value in 1..4.

    Say it out loud

    It fails every time. Rand_mode zero freezes len at ten, but the constraints that mention len stay active and are checked against that frozen value. The inline sets is_wr to one, which requires len in one to four, and ten does not satisfy that, so randomize returns zero and len stays ten. The trap is thinking a frozen variable is excluded from the constraints. Next I would choose is_wr equal to zero, where ten lies in eight to twelve, or release len with rand_mode one.

    Related topics

    • rand_mode(): Freezing Fields

    • Inline Constraints: randomize() with

    • Back to randomize() with Puzzles

    • Randomization & Constraint Puzzles section

    Previous

    108 / 293

    Next: pre_randomize Assignment vs Solver and post_randomize Call Count

    Up next

    Up nextpre_randomize Assignment vs Solver and post_randomize Call Count

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Inline Implication That Looks Like a Conflict
    • Constraint Override by Name in a Derived Class
    • unique and Array Element Counting