RTL Studio Open IDE

3.5 · SYSTEMVERILOG CLASS

Constraint Random (rand/randomize)

Generate unpredictable stimulus with rand and randomize() to hit corner-case bugs.

Watch on YouTube

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 rand keyword inside a class.
  • Call the randomize() method to assign random values to rand variables.
  • 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.

FeatureDescription
randDeclares 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.
classContainer 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

  1. Open this chapter in RtlStudio.

Confirm student/rtl/design.sv and student/tb/tb.sv appear in Explore.

  1. Open student/tb/tb.sv in the editor.
  2. In the for loop, replace st.__TODO__() with the correct method call to generate random values.
  3. Run Simulation from the left sidebar and check the Log panel for expected output.
  4. If needed, open the Waveform tab to inspect data_in values 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

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. 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.