Random and Constrained-Random Stimulus

Let the computer choose the inputs. See why random stimulus finds bugs nobody listed, and why constraints must describe what is legal. Then replay a failure exactly from its seed.

You Will Be Able To

  • Explain why random stimulus finds bugs that a directed list misses.
  • Say why random stimulus needs a reference model to know each answer.
  • Describe a constraint as the statement of which inputs are legal.
  • Use a seed to replay a random failure exactly, and say what to record when one fails.
Lesson
B3.2
Time
22 min
Runs on
Your browser
Check
5 questions

Six random runs of the ALU, four hundred draws each. Five pass. One prints a failure at draw 270, on an addition nobody would have picked: 3 + 253. Run it again tomorrow, and draw 270 fails again in exactly the same way.

Darts on a Board, Then a Board with Legal RegionsStep through. Random inputs land all over the input space, far from where a person would aim. Constraints mark the legal region, so every dart is a stimulus the design may really receive.
  • Control / FSM
  • Step
  • DUT
  • Model
  • Test
  • Data
Darts on a Board, Then a Board With Legal RegionsStep through. Random inputs land all over the input space, far from where a person would aim. Constraints mark the legal region, so every dart is a stimulus the design may really receive. control Seed one number that fixesevery later draw control Random generator draws op, a and b step Constraint op is 0 to 3; 4 to 7are reserved dut alu8 the design under test model Reference model the right answer forany draw test Compare first mismatch: seedand draw number startskeeps draws legalop, a, bsame drawy, c, zexpected
  1. control Random generatordraws op, a and b
    • op, a, b → alu8
    • same draw → Reference model
  2. dut alu8the design under test
    • y, c, z → Compare
  3. model Reference modelthe right answer for any draw
    • expected → Compare
  4. test Comparefirst mismatch: seed and draw number
  5. step Constraintop is 0 to 3; 4 to 7 are reserved
    • keeps draws legal → Random generator
  6. control Seedone number that fixes every later draw
    • starts → Random generator
  1. Throw darts

    A generator draws inputs nobody chose by hand, so they land in places nobody would aim.

  2. Know every answer

    A random input has no hand-worked answer, so a reference model works it out for each draw.

  3. Mark the legal region

    A constraint says which draws the design may really receive. Here: op 0 to 3 only.

  4. Keep the seed

    The same seed gives the same darts in the same order, so any failure can be thrown again.

Read the diagram as text
  • Seed: one number that fixes every later draw
  • Random generator: draws op, a and b
  • Constraint: op is 0 to 3; 4 to 7 are reserved
  • alu8: the design under test
  • Reference model: the right answer for any draw
  • Compare: first mismatch: seed and draw number
  • Seed → Random generator: starts
  • Constraint → Random generator: keeps draws legal
  • Random generator → alu8: op, a, b
  • Random generator → Reference model: same draw
  • alu8 → Compare: y, c, z
  • Reference model → Compare: expected

An architecture drawing of the lab's bench. Arrows carry one draw from the generator to the comparison.

Before You Start

Read Directed Tests first: the same ALU and the same bug appear here. A reference model is a second way to know an answer; Where the Right Answer Comes From introduced it.

Five questions this page answers
Question
1Why does random stimulus find bugs that nobody listed?
2How does a random test know the right answer?
3What does a constraint describe?
4What is a seed, and why must a failing seed be recorded?
5What goes wrong when random stimulus is not constrained?

The Idea: Throw Darts, Then Keep Them on the Board

In the last lesson, five directed cases passed while 255 legal inputs gave wrong answers. People aim their cases at the features they can name. A bug at a seam sits where nobody aims. A random generator does not aim at all, so it lands in places no person would choose.

Random stimulus brings two new needs. First, no person worked out the answer for draw 270. So the bench needs a reference model that computes the right answer for any input. Second, not every random value is legal. The ALU's specification reserves operations 4 to 7. A draw of op 6 tests nothing the chip will ever receive. A failure there is the bench's fault, not the design's.

So the generator gets rules: constraints. A constraint describes the legal region of the input space, and the generator draws only inside it. Random plus constraints is called constrained-random stimulus. It is how most of a modern design's input space gets visited.

New Words

New words on this page
WordMeaning
Random stimulusInputs drawn by a generator instead of chosen by a person.
Reference modelCode that computes the right answer for any input, so random draws can be checked.
ConstraintA rule that says which values are legal. The generator draws only values that keep every rule.
Constrained-randomRandom stimulus that keeps to the constraints: random inside the legal region.
SeedThe starting number of a random generator. The same seed gives the same sequence of draws.
ReservedA value the specification forbids or gives no meaning. Here: op codes 4 to 7.
ReplayRunning a failing test again with its seed, to see the same failure in the same place.

The Smallest Example: Odds by Hand

Before running anything, estimate how often a random draw hits the bug. The bug needs an ADD whose two operands sum to exactly 256.

How often one random draw hits the bug
EventChanceWhy
op is ADD1 in 4four legal operations, drawn evenly
a + b = 256255 in 65,536a from 1 to 255 each has one partner b
both at onceabout 1 in 1,0284 x 65,536 / 255
at least one hit in 400 drawsabout 1 in 31 - (1 - 1/1,028) to the power 400

So roughly one run in three of 400 draws should find the bug. A person writing cases by hand might never find it at all. The generator needs no idea where the bug is; it only needs enough draws.

The Rules

  1. Rule 1: Random needs a model.

    Every random draw is checked against a reference model, because nobody worked its answer out by hand.

  2. Rule 2: Constraints describe what is legal.

    Write down which inputs the design may receive, and draw only those. An illegal draw makes a false failure.

  3. Rule 3: A seed fixes the sequence.

    The same seed on the same simulator gives the same draws in the same order.

  4. Rule 4: Record the seed of every run.

    A random failure you cannot replay is a failure you cannot debug.

  5. Rule 5: Many seeds, not one long seed.

    Each new seed is a new walk through the space. Regressions run many of them.

Sources for These Rules

Constrained-random stimulus, a model that predicts the answer, many seeds: Spear and Tumbush, section 1.6.

What to randomize beyond data (configuration, protocol, errors, delays): Spear and Tumbush, section 1.7.

Constrained random verification as a method, overview: Mehta, ASIC/SoC Functional Design Verification, sections 5.1 and 5.2.

$urandom and $urandom_range, and that one seed gives one sequence: IEEE 1800-2023, clauses 18.13.1 and 18.13.2.

The Lab: Six Seeds and a Replay

The lab runs alu8 under six seeds, 400 draws each, and stops a run at its first mismatch. Then it replays the failing seed. This is the heart of one run.

The call $urandom returns a random 32-bit number, cut to the width of its target. The call $urandom(seed) starts the sequence from the seed. The call $urandom_range(3, 0) draws from 0 to 3.
seed = s;
r = $urandom(seed);              // start the generator from this seed
for (int i = 1; i <= 400 && bad == 0; i++) begin
  op = $urandom_range(3, 0);     // only the four legal operations
  a = $urandom;
  b = $urandom;
  #1 if ({y, c, z} !== model(op, a, b)) bad = i;
end
Printed by the lab (Icarus Verilog version 12.0).
seed 1: FAIL at draw 270: op=0 a=3 b=253 gave y=0 c=1 z=0
seed 2: 400 draws, all pass
seed 3: 400 draws, all pass
seed 4: 400 draws, all pass
seed 5: 400 draws, all pass
seed 6: 400 draws, all pass
replay of seed 1: FAIL at draw 270: op=0 a=3 b=253 gave y=0 c=1 z=0

Rule 5 at work: one seed in six found the bug, close to the one-in-three estimate. Seed 1 failed at draw 270 on 3 + 253, which is 256. The design gave z = 0 where the specification wants 1. Rule 3 at work: the replay fails at the same draw with the same numbers.

Now remove the constraint and draw op from all eight codes. Every seed fails within a few draws, as in seed 1: FAIL at draw 2: op=6 a=199 b=198 gave y=0 c=0 z=1. Op 6 is reserved. The model and the design disagree about something the chip will never be asked. The real bug is buried under false failures.

Why Many Short Seeds and Written Constraints

Why not just run one enormous random test? Because a failure in draw nine million is hard to debug, and one sequence explores only one path. Many short runs with different seeds explore more paths, and each failure comes with a short, replayable story.

Why write constraints instead of filtering bad draws afterwards? Because the constraints are the specification of the stimulus. They can be read, reviewed, and changed for one test without touching the rest. Languages such as SystemVerilog give constraints their own syntax for this reason.

Traps

The first trap is a failure without its seed. A regression prints FAIL, the log keeps no seed, and nobody can make the bug happen again. It may take weeks to see it once more. Print the seed at the start of every run, before anything can go wrong.

The second trap is the over-tight constraint. Someone limits a and b to small numbers to stop "noise", and every sum stays under 256. The carry bug can never be drawn, and every seed passes. A constraint that removes legal inputs hides bugs as surely as a missing test.

The third trap is assuming a seed works across tools. The same seed gives the same draws on the same simulator, but different simulators use different generators. Record the tool and its version with the seed.

Where This Goes Next

Random stimulus is the engine of most benches. These pages teach the language and the method behind it.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Constraints with their own syntax, in a classRand Fields and Checked Randomization
Ranges, implication and weights in constraintsInside, Implication and Distributions
Keeping a seed stable as a bench growsRandom Stability and Callbacks
Seeds and directed closure in a UVM regressionSeeds, Coverage and Directed Closure
Thinking of the inputs random draws rarely makeCorner Cases and How to Think of Them

Try It

Run it: six seeds of 400 draws, then a replay of the first failing seed.

Change the constraint to op = $urandom; and run again. Why does every seed fail, and is any failure a real bug?

Fix the zero flag in alu8 so it looks at y only. What do all six seeds print now?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: seed 1: FAIL at draw 270: op=0 a=3 b=253 gave y=0 c=1 z=0 seed 2: 400 draws, all pass seed 3: 400 draws, all pass seed 4: 400 draws, all pass seed 5: 400 draws, all pass seed 6: 400 draws, all pass replay of seed 1: FAIL at draw 270: op=0 a=3 b=253 gave y=0 c=1 z=0

Check Yourself

Predict the outputThe lab now runs the seeds in the order 2, 3, 4, 5, 6, 1. What is the last line it prints?
// alu8: an 8-bit ALU. op: 0 ADD, 1 SUB, 2 AND, 3 XOR; 4 to 7 are reserved.
// c is the carry out of ADD or the borrow of SUB. z is 1 when y is 0.
module alu8 (input logic [2:0] op, input logic [7:0] a, b,
             output logic [7:0] y, output logic c, z);
  logic [8:0] r;                    // nine bits: the result and its carry
  always_comb begin
    case (op)
      3'd0: r = a + b;
      3'd1: r = a - b;
      3'd2: r = {1'b0, a & b};
      3'd3: r = {1'b0, a ^ b};
      default: r = 9'd0;
    endcase
  end
  assign y = r[7:0];
  assign c = r[8];
  assign z = (r == 9'd0);       // the zero flag
endmodule

module tb;
  logic [2:0] op; logic [7:0] a, b, y; logic c, z;
  int seed, found = 0;
  int unsigned r;
  alu8 dut (.op(op), .a(a), .b(b), .y(y), .c(c), .z(z));
  // The reference model: the answer the specification gives, worked out a second way.
  function automatic logic [9:0] model(input logic [2:0] op, input logic [7:0] a, b);
    logic [7:0] y; logic c;
    case (op)
      3'd0: begin y = a + b; c = (a + b) > 255; end
      3'd1: begin y = a - b; c = a < b; end
      3'd2: begin y = a & b; c = 0; end
      default: begin y = a ^ b; c = 0; end
    endcase
    return {y, c, y == 0};
  endfunction
  // One random run: start the generator from a seed, then draw and check.
  task automatic run(input int s, input string tag);
    int bad = 0;
    seed = s;
    r = $urandom(seed);             // start the generator from this seed
    for (int i = 1; i <= 400 && bad == 0; i++) begin
      op = $urandom_range(3, 0);   // only the four legal operations
      a = $urandom;
      b = $urandom;
      #1 if ({y, c, z} !== model(op, a, b)) begin
        bad = i;
        $display("%s %0d: FAIL at draw %0d: op=%0d a=%0d b=%0d gave y=%0d c=%0d z=%0d", tag, s, i, op, a, b, y, c, z);
      end
    end
    if (bad == 0) $display("%s %0d: %0d draws, all pass", tag, s, 400);
    else if (found == 0) found = s;
  endtask
  initial begin
    run(2, "seed");
    run(3, "seed");
    run(4, "seed");
    run(5, "seed");
    run(6, "seed");
    run(1, "seed");
    if (found != 0) run(found, "replay of seed");
  end
endmodule
ConceptWhy does a random test need a reference model?
ConceptWhat does a constraint describe?
ConceptA random run fails at draw 270 with seed 1. What lets you see the same failure tomorrow?
ConceptWithout the constraint, every seed fails on a reserved op code. What went wrong?

Common Mistakes

Wrong Run random tests without printing the seed.

A failure that cannot be replayed cannot be debugged.

Fix Print the seed, the tool and its version at the start of every run.

Wrong Draw from every possible value, legal or not.

Reserved and illegal inputs cause failures that are not bugs, and bury the real ones.

Fix Write constraints for the legal region and draw only inside it.

Wrong Narrow the constraints to make failures go away.

Legal inputs that are never drawn are never tested.

Fix Constrain to what the specification allows, no tighter.

Remember

  • Random stimulus visits inputs no person would choose, which is where seam bugs hide.
  • Every random draw needs a reference model, because nobody worked its answer out by hand.
  • Constraints describe the legal inputs; an unconstrained draw causes false failures.
  • The same seed gives the same draws on the same simulator; record it with every run.

Practice

Report a problem with this page