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.
- Control / FSM
- Step
- DUT
- Model
- Test
- Data
- Throw darts
A generator draws inputs nobody chose by hand, so they land in places nobody would aim.
- Know every answer
A random input has no hand-worked answer, so a reference model works it out for each draw.
- Mark the legal region
A constraint says which draws the design may really receive. Here: op 0 to 3 only.
- 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.
| Question | |
|---|---|
| 1 | Why does random stimulus find bugs that nobody listed? |
| 2 | How does a random test know the right answer? |
| 3 | What does a constraint describe? |
| 4 | What is a seed, and why must a failing seed be recorded? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Random stimulus | Inputs drawn by a generator instead of chosen by a person. |
| Reference model | Code that computes the right answer for any input, so random draws can be checked. |
| Constraint | A rule that says which values are legal. The generator draws only values that keep every rule. |
| Constrained-random | Random stimulus that keeps to the constraints: random inside the legal region. |
| Seed | The starting number of a random generator. The same seed gives the same sequence of draws. |
| Reserved | A value the specification forbids or gives no meaning. Here: op codes 4 to 7. |
| Replay | Running 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.
| Event | Chance | Why |
|---|---|---|
| op is ADD | 1 in 4 | four legal operations, drawn evenly |
| a + b = 256 | 255 in 65,536 | a from 1 to 255 each has one partner b |
| both at once | about 1 in 1,028 | 4 x 65,536 / 255 |
| at least one hit in 400 draws | about 1 in 3 | 1 - (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
Rule 1: Random needs a model.
Every random draw is checked against a reference model, because nobody worked its answer out by hand.
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.
Rule 3: A seed fixes the sequence.
The same seed on the same simulator gives the same draws in the same order.
Rule 4: Record the seed of every run.
A random failure you cannot replay is a failure you cannot debug.
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.
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;
endseed 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=0Rule 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.
| The idea from this page | Where it is taught |
|---|---|
| Constraints with their own syntax, in a class | Rand Fields and Checked Randomization |
| Ranges, implication and weights in constraints | Inside, Implication and Distributions |
| Keeping a seed stable as a bench grows | Random Stability and Callbacks |
| Seeds and directed closure in a UVM regression | Seeds, Coverage and Directed Closure |
| Thinking of the inputs random draws rarely make | Corner 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?
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=0Check Yourself
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.