VLSI DV Interview Puzzles · All levels

pre_randomize Assignment vs Solver and post_randomize Call Count

Predict outcomes of both calls: final len range for Call A and whether post_count increments on Call B.

Puzzle

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

Predict outcomes of both calls: final len range for Call A and whether post_count increments on Call B.

Code

systemverilog
class life_req;
  rand bit [3:0] len;
  bit force_short;
  int post_count;
  constraint c_base { len inside {[1:8]}; }

  function void pre_randomize();
    if (force_short) len = 2;
  endfunction

  function void post_randomize();
    post_count++;
  endfunction
endclass

life_req r = new();
r.force_short = 1;
assert(r.randomize() with { len inside {[5:6]}; }); // Call A
void'(r.randomize() with { len == 9; });            // Call B

Hint

pre_randomize runs before solving; post_randomize runs only on success.

Step-by-step solution

diagram
1) Call A: pre_randomize assigns len=2, but solver then enforces c_base and inline len in 5..6.
2) So Call A succeeds with len either 5 or 6; tuple-count gives 50/50.
3) post_randomize runs after successful Call A, so post_count increments by 1.
4) Call B asks len==9, conflicting with c_base len 1..8 -> fail.
5) Because Call B fails, post_randomize is not invoked for that call.

Answer

Answer: Call A succeeds with len in {5,6} (about 50/50), then Call B fails, and post_count increments only once total.

Why candidates get it wrong

Candidates often think pre_randomize assignment pins the rand value and that post_randomize runs even on failed randomize.

Interviewer follow-up

How would you intentionally lock len to 2 for Call A without causing solve failure?

Related topics