randomize() Vs std::randomize()
In short: The class method randomize() assigns random values to the rand and randc members of an object, using that class’s constraints. The scope function std::randomize() does the same job but for plain variables in the current scope, with no class needed. Use randomize() when your random data lives in a class with reusable constraints; use std::randomize() for a quick local randomization inside a task or module. Both return 1 on success and 0 on failure.
SystemVerilog gives you two front doors to the same constraint solver. One works on class members, the other on loose variables in the current scope. Knowing which to reach for keeps small jobs simple and large jobs reusable.
A simple way to picture it
Think of two ways to plan a meal. The class method is like a recipe card you keep in a box: it lists ingredients and rules once, and you reuse and adjust it across many dinners. The scope function is like grabbing whatever is in the fridge right now and deciding on the spot. The recipe card is reusable and shareable; the fridge grab is quick and local. Both feed you, but you pick based on whether you will use the plan again.
The class randomize() method
Every class has a built in virtual method randomize(). It sets all active rand and randc members subject to the class constraints. Because the data and its rules live in the class, you can extend, inherit, and override them, which is why class based randomization scales to large environments.
class packet;
rand bit [7:0] addr;
rand bit [7:0] len;
constraint c_len { len inside {[1:64]}; }
endclass
module tb;
initial begin
packet p = new();
if (p.randomize())
$display("addr=%02h len=%0d", p.addr, p.len);
else
$display("randomize failed");
end
endmoduleThe scope std::randomize() function
The scope function std::randomize() randomizes variables in the current scope without a class. You list the variables to randomize as arguments. It behaves like the class method, but on local variables, which is handy for a quick calculation inside a task.
module tb;
initial begin
int a, b;
if (std::randomize(a, b))
$display("a=%0d b=%0d", a, b);
else
$display("scope randomize failed");
end
endmoduleAdding constraints with std::randomize() … with
You can attach inline constraints to a scope randomize using the with clause. The variables you pass as arguments become random; every other variable in the expression is treated as a fixed state variable. This lets you constrain local values without writing a class at all.
task gen_pair(int limit);
int x, y;
// x and y are random; limit is a state variable
if (std::randomize(x, y) with { x < y; x + y < limit; }) begin
$display("x=%0d y=%0d (sum < %0d)", x, y, limit);
end
// A second, independent call with a different rule
if (std::randomize(x, y) with { y - x > limit; }) begin
$display("x=%0d y=%0d (diff > %0d)", x, y, limit);
end
endtask
module tb;
initial gen_pair(50);
endmoduleEach call solves on its own, so the two calls give two separate sets of values for x and y. In the first call x is less than y and their sum is under the limit; in the second their difference is greater than the limit.
Random variables vs state variables
This split matters. In a scope randomize, only the arguments are solved for; anything else in the with block is read as a constant during that call. Naming the wrong set of arguments is a common cause of surprising results.
Side by side comparison
| Point | randomize() (class) | std::randomize() (scope) |
|---|---|---|
| Works on | rand and randc class members | Variables in the current scope |
| Needs a class? | Yes | No |
| Constraints | Class constraint blocks | Inline with clause |
| Reuse and inheritance | Yes, extend and override | No, local to the call |
| Return value | 1 on success, 0 on failure | 1 on success, 0 on failure |
| Best for | Reusable stimulus objects | Quick local randomization |
Expected output, in plain words
The class example prints an address and a length between 1 and 64. The scope example prints two random integers. The with example prints two lines: the first with x less than y and a sum under 50, the second with a difference greater than 50. Exact numbers vary by seed.
Note: these outputs describe what the code is written to produce from a read of the IEEE 1800 LRM. Run on your own simulator or EDA Playground to confirm.
When to use which
- Use the class method when the random data belongs to a transaction or config object you reuse across tests, and you want constraints you can extend and override.
- Use the scope function for a quick, one off randomization of local variables inside a task or module, where a class would be overkill.
- Use the with clause to add short inline rules to a scope randomize without defining a class.
Common mistakes to avoid
- Ignoring the return value: both forms return 0 on failure. Always test it.
- Wrong argument list in scope randomize: only the arguments are randomized; a variable you forgot to pass stays fixed.
- Expecting inheritance from a scope randomize: it is local to the call and cannot be extended like class constraints.
- Reaching for a class when a scope call is simpler: for a quick local value, std::randomize keeps the code short.
To control which class members take part, see our guides on constraint override and enable and disable constraint. For the difference between rand and randc, read randc behavior from a rand variable. More is in our SystemVerilog tutorials.
Frequently asked questions
What is the difference between randomize() and std::randomize()?
randomize() is a class method that sets the rand and randc members of an object using the class constraints. std::randomize() is a scope function that randomizes plain variables in the current scope with no class needed.
When should I use std::randomize()?
Use it for a quick, local randomization of variables inside a task or module, where creating a class would be unnecessary. Add inline rules with the with clause.
What does the with clause do in std::randomize()?
It attaches inline constraints to the scope randomize. The variables passed as arguments become random; any other variable in the expression is treated as a fixed state variable.
What is the difference between random and state variables in a scope randomize?
Only the arguments you pass are solved for and count as random. Every other variable in the with block is read as a constant during that call.
Do both forms return a success value?
Yes. Both randomize() and std::randomize() return 1 on success and 0 on failure. Always check the return value so a failed solve does not go unnoticed.
Can std::randomize() use class inheritance?
No. A scope randomize is local to the call and cannot be extended or overridden. For reusable, inheritable constraints, use class based randomize().


