Module 1: SystemVerilog for UVM | Beginner UVM

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.

Verification mindset: Instead of asking, "What values should I test?", randomization lets us explore many possible values automatically.

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.

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.

Basic pattern:
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.

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.

Important distinction: We are randomizing the same object repeatedly here. We are not creating ten objects.

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.

Remember from Lesson 3: 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.

Think of it this way:

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
Verification mindset: Random does not mean unpredictable forever. A good verification environment must be able to reproduce interesting random scenarios.

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.

UVM connection: Later, your UVM sequences will commonly create or receive transaction objects, randomize them, and send them toward a driver.

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.

Important: Do not think of randomization as a UVM feature. Randomization is a SystemVerilog language feature that UVM uses.

Try it yourself

Create a class called transaction with three random properties:

  • addr — 8 bits
  • data — 32 bits
  • write — 1 bit

Then:

  1. Create one transaction object.
  2. Call randomize() five times.
  3. Print the values after every successful randomization.
  4. Check the return value of randomize().
Challenge: Create two transaction objects. Randomize both objects and print their values. Then explain why changing one object's values does not change the other object's values.

Quick knowledge check

Question 1

Which keyword marks a class property as randomizable?

  1. random
  2. rand
  3. randomize
  4. constraint
Answer: B — rand marks a property as randomizable.

Question 2

Which method is used to randomize an object?

  1. random()
  2. generate()
  3. randomize()
  4. rand()
Answer: C — Call randomize() on the object.

Question 3

What does this code do?

if (!tr.randomize())


$fatal("Randomization failed");
  1. Always stops the simulation
  2. Checks whether randomization failed
  3. Creates a new transaction
  4. Copies the transaction
Answer: B — It checks the return value of randomize() and stops the simulation if randomization fails.

Question 4

Does declaring a property with rand immediately generate a random value?

  1. Yes
  2. No
Answer: B — You must call randomize().

Question 5

What is the purpose of constraints?

  1. To create objects
  2. To define legal rules for random values
  3. To connect a driver to a DUT
  4. To create UVM phases
Answer: B — Constraints control which randomized values are legal.

Common beginner mistakes

1. Using 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.

2. Not checking the return value

Develop the habit of checking randomize(), especially once constraints are introduced.

3. Confusing randomization with constraints

Randomization generates values. Constraints restrict which values are legal. They work together, but they are not the same concept.

4. Assuming random means impossible to reproduce

Random simulations can be reproduced using the appropriate random seed and simulation setup.

5. Randomizing the wrong object

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 rand keyword marks a class property as randomizable.
  • The randomize() method generates values for random properties.
  • Declaring rand does 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.