Skip to main content...
S3 · Verification Engineering (SV + UVM)
30 min

Day 100: Constrained randomization I: rand and constraint blocks

Constrained randomization is DV's superpower: describe the legal input space and let the tool generate thousands of valid, surprising stimuli.

Constrained randomization I

Instead of hand-writing every test, you describe the legal input space and let the solver generate it. Mark fields `rand`, then write `constraint` blocks expressing the rules (valid ranges, relationships, weightings). Calling randomize() produces a random-but-legal value each time. Thousands of these explore corners you'd never enumerate by hand — the essence of constrained-random verification.

rand fields with constraints
class axi_txn;
    rand bit [31:0] addr;
    rand bit [31:0] data;
    rand bit        is_write;
    rand int        len;

    constraint c_addr  { addr inside {[32'h1000:32'h5FFF]}; }  // valid range
    constraint c_align { addr[1:0] == 2'b00; }                 // word-aligned
    constraint c_len   { len inside {[1:16]}; }                // burst length

    // usage:  axi_txn t = new(); assert(t.randomize());
endclass

Constraints encode the spec

A good constraint set is a machine-checkable encoding of the interface spec: 'addresses are word-aligned and in range', 'bursts are 1–16 beats'. The solver then generates only *legal* stimulus, but with far more variety than a human. When a constraint is too loose you generate illegal inputs; too tight and you miss corners — tuning that balance is a real DV skill.

Key terms

rand
A modifier marking a class field for randomization by randomize().
constraint block
A declarative set of rules the solver must satisfy when randomizing.
randomize()
The method that assigns rand fields a random solution meeting all constraints.
inside
A constraint operator restricting a value to a set or range.

Before moving on, you should be able to

What is the core idea of constrained-random verification?

We use cookies

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Learn more

    Day 100: Constrained randomization I: rand and constraint blocks | RBTechIconX