← Design Verification Interviews

Interview Question · Concept

Rand Versus Randc

L0 FresherL1 Juniorsystemverilograndomization

The Question

The interviewer asks

What is the difference between a rand and a randc variable? When would you pick randc?

Reveal the Model Answer

A rand variable is drawn independently on every call, so a value can repeat straight away. A randc variable is cyclic. It visits every allowed value once, in random order, before any value repeats. Then it starts a new cycle. Pick randc for a small set you want covered quickly, such as an opcode or a port number. Use rand for everything else.

Where a Randc Variable Keeps Its CycleOne randomize() call on an object with a randc field. Arrows show the solver taking and returning values. A rand field has no such list and draws from every legal value on each call.
Where a randc variable keeps its cycleOne `randomize()` call on an object with a `randc` field. Arrows show the solver taking and returning values. A `rand` field has no such list and draws from every legal value on each call. component randomize() called on the sameobject queue Values left inthis cycle stored inside theobject component Pick one atrandom only from the valuesleft component op gets the value no repeat until thecycle ends component List empty: newcycle every legal value goesback in choose fromremove itrefill
Read the diagram as text
  • randomize(): called on the same object
  • Values left in this cycle: stored inside the object
  • Pick one at random: only from the values left
  • op gets the value: no repeat until the cycle ends
  • List empty: new cycle: every legal value goes back in
  • randomize() → Pick one at random
  • Values left in this cycle → Pick one at random: choose from
  • Pick one at random → op gets the value
  • Pick one at random → Values left in this cycle: remove it
  • List empty: new cycle → Values left in this cycle: refill

The list lives in the object, so a new object starts a new cycle.

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Fresher)

A good fresher answer states the difference: rand values can repeat, while randc values cycle. It gives a small enumerated field, such as an opcode, as the place for randc.

Cross-Questions

Where the interviewer goes next. Open a node to practise that follow-up.

WhyBreak ItCompareScale It

  1. Rand Versus Randc
  2. Break ItA Randc Field That Kept RepeatingL1 Junior

    The opcode field is randc, yet the log shows the same opcode twice in a row. The generator does tr = new; tr.randomize(); for every transaction. Why?

    log
    gen: op=2
    gen: op=2
    gen: op=0
    gen: op=3
    Reveal the Model Answer

    The cycle state lives in the object, and every new starts a fresh cycle with its first value chosen at random. Randomize one generator object repeatedly and copy its result into each transaction, or keep the randc field in a long-lived object.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "randc remembers its cycle for the whole simulation."

    It remembers per object. The twin constructs a new object for every call and repeats appear; one object for the whole test removes them.

    Run It

    A new randc object for every call, ten windows of four.

    RUNVerilator · simulator service
    Edit the code, then run it
    Expected: FAIL: each new object starts its own randc cycle, so values repeated within four calls
    See the Fix
    class opcode_gen;
      randc bit [1:0] op;
    endclass
    module tb;
      opcode_gen g;
      int repeats = 0;
      initial begin
        g = new;                          // the fix: one generator object for the whole test
        repeat (10) begin
          bit [3:0] seen;
          seen = 0;
          repeat (4) begin
            void'(g.randomize());
            seen[g.op] = 1;
          end
          if (seen != 4'hf) repeats++;
        end
        if (repeats > 0) $display("FAIL: each new object starts its own randc cycle, so values repeated within four calls");
        else $display("PASS: one object kept one cycle, so every window covered all four values");
      end
    endmodule
    

    Prints: PASS: one object kept one cycle, so every window covered all four values

  3. WhyWhy Randc Fields Stay SmallL1 Junior

    Why not declare a 32-bit address randc?

    Reveal the Model Answer

    The solver has to remember which values of the current cycle are used, which costs memory per value of the range. So the standard lets tools limit the width of a randc variable, but not below 8 bits. A 32-bit randc may be rejected and is not portable. For addresses, a constraint plus coverage does the job.

    Self-Check

    Tick each key point you covered in your answer.

    Spec Misquote

    "randc works on any width; it just takes longer."

    Tools are allowed to cap randc width, and only 8 bits is guaranteed.

    Source IEEE 1800-2017 §18.4.2

  4. WhyWhy Randc Variables Are Solved FirstL2 Mid-Level

    Why are randc variables solved before the rand variables, and what can that break?

    Reveal the Model Answer

    To keep its cycle, the solver picks the randc value first, from the values not yet used. Then it solves the rand fields around it. If a constraint ties the rand fields to that value and no solution exists, the call fails. So limiting a randc field through other fields is fragile.

    Self-Check

    Tick each key point you covered in your answer.

    Spec Misquote

    "Add solve len before addr to order a randc field."

    The standard does not allow randc variables in solve...before. They are always solved first.

    Source IEEE 1800-2017 §18.5.10

  5. CompareRandc Versus Shuffling a ListL1 JuniorL2 Mid-Level

    Asked if they mention covering a list of values

    You must visit seven specific register addresses, in random order, exactly once each. Compare randc with filling an array and calling shuffle().

    Reveal the Model Answer

    randc cycles over every value its type and constraints allow. Seven scattered addresses therefore need an index variable, a range constraint and a lookup table. An array of the seven addresses with shuffle() states the intent directly. It works for any element type and gives a new order on every call. Use shuffle() for an explicit list, and randc when the set is a small contiguous range.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "shuffle() takes constraints like randomize()."

    It is an array ordering method with no constraint support; it just permutes the elements in place.

    Source IEEE 1800-2017 §7.12.2

  6. Scale ItWhy Randc Is Not a Coverage StrategyL2 Mid-LevelL3 Senior

    A colleague proposes making every enumerated field randc "so coverage closes automatically". What do you tell them?

    Reveal the Model Answer

    randc guarantees one field's values cycle, but bugs live in combinations and sequences: opcode with length, back-to-back writes, a command during reset. Those are crosses and transitions that no per-field cycle controls. Close coverage by measuring the coverage model, finding holes and steering constraints or adding directed sequences at them.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Senior)

    A good senior answer moves the talk from stimulus tricks to the coverage model. It names crosses and transitions as the items that close last.

    Wrong Altitude

    Agrees, and adds randc to the remaining fields.

    It optimises the easy part, single-field values, and leaves the cross coverage that closes late untouched.

What People Get Wrong

Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.

Misconception

"Four calls on a 2-bit rand field give each value once."

Each call is independent, so repeats within a window of four are the common case. The twin counts windows with a repeat; switching the field to randc makes the count zero.

Run It

Ten windows of four calls on a 2-bit rand field.

RUNVerilator · simulator service
Edit the code, then run it
Expected: FAIL: rand draws each call independently, so values repeat within four calls
See the Fix
class opcode_gen;
  randc bit [1:0] op;    // the fix: randc cycles through all four values
endclass
module tb;
  opcode_gen g = new;
  int repeats = 0;
  initial begin
    repeat (10) begin                 // ten windows of four calls
      bit [3:0] seen;
      seen = 0;
      repeat (4) begin void'(g.randomize()); seen[g.op] = 1; end
      if (seen != 4'hf) repeats++;
    end
    $display("windows of four calls with a repeated value: %0d of 10", repeats);
    if (repeats > 0) $display("FAIL: rand draws each call independently, so values repeat within four calls");
    else $display("PASS: every window of four calls visited all four values");
  end
endmodule

Prints: windows of four calls with a repeated value: 0 of 10 PASS: every window of four calls visited all four values

Half-Truth

"Use randc everywhere; it reaches coverage faster."

For wide fields a cycle is billions of calls long, and constraints between fields still decide which combinations appear. It speeds up one small field, nothing more.

Buzzword Without Mechanism

Says "randc is cyclic" but cannot say when the cycle restarts or who owns it.

The useful part is the mechanism. The object remembers which values it has used, and a new cycle starts after the last one.

Learn It, Then Prove It