Randomization Basics
Learn how SystemVerilog automatically generates stimulus values so you can explore many verification scenarios without writing every value by hand.
Learning objectives
- Explain why randomization is useful in verification.
- Declare randomizable class properties using
rand. - Randomize an object using
randomize(). - Check whether randomization succeeded or failed.
- Generate multiple randomized transactions.
- Understand the difference between randomization and constraints.
- Understand why reproducible random stimulus is important for debugging.
- Explain how randomization will be used later in UVM sequences.
Step 1: Why do verification engineers need randomization?
A verification environment needs to test many different scenarios. Testing only one manually selected transaction is rarely enough.
Imagine a bus transaction containing:
addr = 8'h10
data = 32'h1234_5678
write = 1
We could manually create another transaction:
addr = 8'h24
data = 32'hABCD_0001
write = 0
But what if we need 1,000 different transactions?
Writing every combination manually would be slow and would probably miss important scenarios.
SystemVerilog provides randomization to automatically generate different values for variables.
Step 2: The rand keyword
A class property can be declared as randomizable
by using the rand keyword.
class packet;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
endclass
The keyword rand tells SystemVerilog that these
properties may receive random values when the object is randomized.
Notice that the class itself has not generated any random values yet.
rand does not immediately
randomize it. You must call randomize().
Step 3: The randomize() method
To generate random values for the rand properties,
call randomize() on the object.
class packet;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
endclass
packet pkt;
initial begin
pkt = new();
pkt.randomize();
$display("addr = %h", pkt.addr);
$display("data = %h", pkt.data);
$display("write = %b", pkt.write);
end
Every time randomize() is called, SystemVerilog can
generate a new set of values for the random properties.
object = new();
object.randomize(); Step 4: Randomize the same object multiple times
You can call randomize() multiple times on the same object.
class packet;
rand bit [7:0] addr;
rand bit [31:0] data;
endclass
packet pkt;
initial begin
pkt = new();
pkt.randomize();
$display("addr=%h data=%h", pkt.addr, pkt.data);
pkt.randomize();
$display("addr=%h data=%h", pkt.addr, pkt.data);
pkt.randomize();
$display("addr=%h data=%h", pkt.addr, pkt.data);
end
Each call generates another random assignment for the randomizable properties.
This is useful when a test needs to generate many different transactions.
Step 5: Always check the return value
The randomize() method returns a value indicating
whether randomization succeeded.
A common verification pattern is:
if (!pkt.randomize()) begin
$display("Randomization failed");
end
Or, when successful randomization is required:
if (!pkt.randomize()) begin
$fatal("Randomization failed");
end
For now, without constraints, randomization of ordinary
rand properties will normally succeed.
Later, constraints can make randomization fail when no legal
solution exists.
randomize() as something
worth checking, especially when constraints are involved.
Step 6: Generate multiple transactions
Randomization becomes much more useful when we generate many transaction objects.
class bus_transaction;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
endclass
bus_transaction tr;
initial begin
tr = new();
repeat (10) begin
if (!tr.randomize())
$fatal("Randomization failed");
$display(
"addr=%h data=%h write=%b",
tr.addr,
tr.data,
tr.write
);
end
end
The repeat (10) loop calls randomize()
ten times.
This gives us ten different stimulus attempts without manually specifying every value.
Step 7: Randomize independent objects
You can also create multiple objects and randomize each one independently.
class packet;
rand bit [7:0] addr;
rand bit [31:0] data;
endclass
packet pkt1;
packet pkt2;
initial begin
pkt1 = new();
pkt2 = new();
if (!pkt1.randomize())
$fatal("pkt1 randomization failed");
if (!pkt2.randomize())
$fatal("pkt2 randomization failed");
$display("pkt1: addr=%h data=%h", pkt1.addr, pkt1.data);
$display("pkt2: addr=%h data=%h", pkt2.addr, pkt2.data);
end
Each object has its own values.
pkt1 and pkt2 are separate objects because
they were created with separate calls to new().
Step 8: Randomization does not mean "anything goes"
Randomization generates values from the legal value space of the randomizable variables.
For example:
rand bit [7:0] addr;
An 8-bit value can represent values from:
0 to 255
So randomization can choose a value from that range.
But what if our protocol says that only addresses from
0x20 through 0x7F are legal?
Randomization by itself does not know that protocol rule.
We will learn how to describe legal values using constraints in the next lesson.
Randomization answers: "Give me a value."
Constraints answer: "Give me a value that follows these rules."
Step 9: Randomization vs. constraints
| Concept | Purpose | Example |
|---|---|---|
| Random property | Marks a property for randomization | rand bit [7:0] addr; |
randomize() |
Generates values for random properties | pkt.randomize(); |
| Constraint | Controls which values are legal | addr inside {[8'h20:8'h7F]}; |
Constraints are intentionally introduced here only as a preview. They are the focus of Lesson 6.
Step 10: Randomization and reproducibility
Random stimulus is powerful, but verification engineers also need to be able to reproduce a failure.
Imagine a random test fails after generating thousands of transactions. You need a way to run the same random scenario again.
SystemVerilog randomization is designed to support repeatable simulation through random seeds.
A simplified mental model is:
same seed
↓
same random sequence
↓
same stimulus
↓
reproduce the failure
The exact way you control and pass simulation seeds depends on your simulator and simulation flow. You will encounter this frequently when debugging constrained-random tests.
Step 11: Randomization in a verification transaction
Let's combine the concepts into a simple transaction class.
class bus_transaction;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
function void print();
$display(
"addr=%h data=%h write=%b",
addr,
data,
write
);
endfunction
endclass
Now generate several transactions:
bus_transaction tr;
initial begin
tr = new();
repeat (5) begin
if (!tr.randomize())
$fatal("Transaction randomization failed");
tr.print();
end
end
This is very close to the type of object you will eventually randomize inside a UVM sequence.
Step 12: What changes when we add UVM?
The basic SystemVerilog idea does not change.
You will still have an object with randomizable properties:
tr.randomize();
UVM provides the architecture around that transaction. A sequence can generate stimulus, a sequencer can control the flow, and a driver can convert transactions into DUT pin activity.
You will learn these components much later in the course.
Try it yourself
Create a class called transaction with three random
properties:
addr— 8 bitsdata— 32 bitswrite— 1 bit
Then:
- Create one transaction object.
- Call
randomize()five times. - Print the values after every successful randomization.
- Check the return value of
randomize().
Quick knowledge check
Question 1
Which keyword marks a class property as randomizable?
randomrandrandomizeconstraint
rand marks a property as randomizable.
Question 2
Which method is used to randomize an object?
random()generate()randomize()rand()
randomize() on the object.
Question 3
What does this code do?
if (!tr.randomize())
$fatal("Randomization failed");
- Always stops the simulation
- Checks whether randomization failed
- Creates a new transaction
- Copies the transaction
randomize() and
stops the simulation if randomization fails.
Question 4
Does declaring a property with rand immediately
generate a random value?
- Yes
- No
randomize().
Question 5
What is the purpose of constraints?
- To create objects
- To define legal rules for random values
- To connect a driver to a DUT
- To create UVM phases
Common beginner mistakes
rand without calling
randomize()
rand bit [7:0] addr;
This only makes the property eligible for randomization. It does not generate a random value by itself.
Develop the habit of checking randomize(),
especially once constraints are introduced.
Randomization generates values. Constraints restrict which values are legal. They work together, but they are not the same concept.
Random simulations can be reproduced using the appropriate random seed and simulation setup.
Make sure the handle refers to a valid object before calling
randomize().
Key takeaways
- Randomization helps verification engineers generate many different stimulus values automatically.
- The
randkeyword marks a class property as randomizable. - The
randomize()method generates values for random properties. - Declaring
randdoes not automatically randomize a property. randomize()returns a status that should be checked.- The same object can be randomized repeatedly.
- Separate objects have separate randomized values.
- Randomization and constraints are different concepts.
- Constraints control which randomized values are legal.
- Random stimulus must be reproducible so failures can be debugged.
- UVM sequences will later use these SystemVerilog randomization features to generate stimulus.
What's next?
Randomization gives us many possible values, but verification engineers usually need more control than simply "pick something."
For example, a protocol might require:
addr must be aligned
write must be 0 or 1
length must be between 1 and 16
some values should occur more often than others
In the next lesson, you will learn how to control randomized stimulus using constraints and distributions.