What you practice
Constraint Random (rand/randomize) (3.5). Generate unpredictable stimulus with rand and randomize() to hit corner-case bugs. It is in Constraint Random Verification — Part 3 · Advanced SV & Mini-UVM of the SystemVerilog Masterclass. Timing, concurrency, constrained-random verification, assertions, and a pure-SV Mini-UVM environment.
The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.
What you will learn
Today you will learn how to generate unpredictable stimulus using SystemVerilog's rand type and the randomize() method to test corner-case behavior in your design.
Learning objectives
- Declare random variables using the
randkeyword inside a class. - Call the
randomize()method to assign random values torandvariables. - Handle
randomize()failure by checking its return value. - Apply generated random values as DUT stimulus in a testbench.
Theory
SystemVerilog provides constraint-based randomization to generate diverse, unpredictable test stimuli.
| Feature | Description |
|---|---|
rand | Declares a variable that can be assigned random values by randomize(). |
randomize() | Method called on an object to assign random values to all rand members. Returns 1 on success, 0 on failure. |
class | Container for rand variables and randomize() logic; must be instantiated with new() before use. |
Randomization is essential for coverage-driven verification, helping uncover rare corner-case bugs that directed tests might miss. The golden testbench calls process::self().srandom(42) so the reference log is reproducible. Without an explicit seed, randomize() values are not guaranteed across runs or simulators.
Practice
- Open this chapter in RtlStudio.
Confirm student/rtl/design.sv and student/tb/tb.sv appear in Explore.
- Open
student/tb/tb.svin the editor. - In the
forloop, replacest.__TODO__()with the correct method call to generate random values. - Run Simulation from the left sidebar and check the Log panel for expected output.
- If needed, open the Waveform tab to inspect
data_invalues over time.
Summary
You practiced generating unpredictable stimulus by declaring a rand variable and calling the randomize() method on a class instance. This approach enables automatic generation of diverse test values to uncover corner-case bugs in your design. The randomize() method returns a status indicating success or failure, allowing you to verify that constraints were met. The key takeaway is that constraint randomization is a powerful technique for creating robust, scalable verification environments.
Run it in the Verilog IDE
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.
The lesson is done when the simulation matches Constraint Random (rand/randomize). Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.