SystemVerilog lets you selectively turn randomization off for individual rand or randc class properties, or for an entire object, using the built-in rand_mode() method. A property with randomization disabled behaves exactly as if it had never been declared rand or randc in the first place — it simply keeps whatever value it already holds the next time randomize() is called.
6 min read | Intermediate Level
What You'll Learn
- How
rand_mode()enables or disables randomization for one variable, or for a whole object - How to check a variable's current
rand_mode()state - What happens to a disabled variable's value when
randomize()is called - How disabling a whole object differs from disabling one variable at a time
Syntax: rand_mode()
rand_mode() can be called either as a task, to set the state, or as a function, to read the current state back.
// Disables randomization of variable [variable_name] inside [class_object] class
[class_object].[variable_name].rand_mode (0);
// Enables randomization of variable [variable_name] inside [class_object] class
[class_object].[variable_name].rand_mode (1);
Parameters:
0— disables randomization for the target (task form)1— enables randomization for the target (task form)- no argument — returns the current state as
1(enabled) or0(disabled) (function form)
This is very similar to the constraint_mode() method used to disable constraint blocks — the difference is that rand_mode() targets a variable, while constraint_mode() targets a constraint block.
Randomization Enabled by Default
By default, every variable declared rand or randc has randomization enabled, as this first example confirms before disabling anything:
// Create a class that contains random variables
class Fruits;
rand bit [3:0] var1;
rand bit [1:0] var2;
endclass
module tb;
initial begin
// Instantiate an object of the class
Fruits f = new();
// Print values of those variables before randomization
$display ("Before randomization var1=%0d var2=%0d", f.var1, f.var2);
// rand_mode() is called as a function which returns the state of the given variable
// If it is enabled, then print a statement
if (f.var1.rand_mode())
if (f.var2.rand_mode())
$display ("Randomization of all variables enabled");
// Randomize the class object which in turn randomizes all internal variables
// declared using rand/randc keywords
f.randomize();
// Print the value of these variables after randomization
$display ("After randomization var1=%0d var2=%0d", f.var1, f.var2);
end
endmodule
Output
ncsim> run Before randomization var1=0 var2=0 Randomization of all variables enabled After randomization var1=15 var2=3 ncsim: *W,RNQUIE: Simulation is complete.
Disabling a Single Variable
Next, we'll disable var1 and see how that affects randomization of that one variable:
// Create a class that contains random variables
class Fruits;
rand bit [3:0] var1;
rand bit [1:0] var2;
endclass
module tb;
initial begin
Fruits f = new();
$display ("Before randomization var1=%0d var2=%0d", f.var1, f.var2);
// Turn off randomization for var1
f.var1.rand_mode (0);
// Print if var1 has randomization enabled/disabled
if (f.var1.rand_mode())
$display ("Randomization of var1 enabled");
else
$display ("Randomization of var1 disabled");
f.randomize();
$display ("After randomization var1=%0d var2=%0d", f.var1, f.var2);
end
endmodule
Output
ncsim> run Before randomization var1=0 var2=0 Randomization of var1 disabled After randomization var1=0 var2=3 ncsim: *W,RNQUIE: Simulation is complete.
var1 stays at its pre-randomization value of 0 even after f.randomize() runs, because rand_mode(0) made the solver treat it as if it were never rand in the first place. var2, left enabled, gets a fresh random value as usual.
Quick Check
Q: You call f.var1.rand_mode(0) and then f.randomize(). What value does var1 have afterward?
Show Answer
Whatever value var1 already had before the call — a disabled variable isn't reset to 0 or any other default, it's simply left untouched by the solver.
Disabling All Variables in an Object
Calling rand_mode() directly on the object, without naming a specific variable, applies to every rand/randc property inside it at once:
// Create a class that contains random variables
class Fruits;
rand bit [3:0] var1;
rand bit [1:0] var2;
endclass
module tb;
initial begin
Fruits f = new();
$display ("Before randomization var1=%0d var2=%0d", f.var1, f.var2);
// Turns off randomization for all variables
f.rand_mode (0);
if (! f.var1.rand_mode())
if (! f.var2.rand_mode())
$display ("Randomization of all variables disabled");
f.randomize();
$display ("After randomization var1=%0d var2=%0d", f.var1, f.var2);
end
endmodule
Output
ncsim> run Before randomization var1=0 var2=0 Randomization of all variables disabled After randomization var1=0 var2=0 ncsim: *W,RNQUIE: Simulation is complete.
With every property disabled, f.randomize() still returns successfully, but neither var1 nor var2 changes — there's nothing left for the solver to randomize.
Where Is rand_mode() Used?
rand_mode() is handy when you want to reuse the same object across multiple randomization calls but freeze part of it — for example, locking a packet's protocol version or destination ID after the first randomization so later calls only randomize the payload, or temporarily disabling a field while debugging a specific constraint failure.
Common Beginner Mistakes
Mistake #1: Expecting a disabled variable's value to reset
Calling rand_mode(0) doesn't clear or reset a variable — it just stops the solver from touching it going forward. Why? The variable keeps whatever value it held at the moment you disabled it, which might be its default, or a value left over from an earlier randomization.
Mistake #2: Assuming rand_mode() affects every instance of the class
rand_mode() is called on a specific object handle, like f.var1.rand_mode(0), and only affects that one object. Why? Each instance of a class tracks its own rand_mode state independently, so disabling randomization on one Fruits object has no effect on any other Fruits object you've created.
Mistake #3: Confusing rand_mode() with constraint_mode()
rand_mode() turns off randomization of a variable; constraint_mode() turns off enforcement of a constraint block. Why? Mixing the two up can leave you disabling the wrong thing — disabling a variable's rand_mode doesn't remove constraints that reference it, and disabling a constraint doesn't stop the variable from being randomized.
Practice Exercise
Challenge: Disable var2 Instead
Using the same Fruits class, write code that disables var2 only, then calls randomize() and prints both variables before and after. What do you expect var2 to be after randomization?
Hint
Follow the same pattern as the var1 example, just swap which variable you call rand_mode(0) on.
Solution
class Fruits;
rand bit [3:0] var1;
rand bit [1:0] var2;
endclass
module tb;
initial begin
Fruits f = new();
$display ("Before randomization var1=%0d var2=%0d", f.var1, f.var2);
// Turn off randomization for var2 this time
f.var2.rand_mode (0);
f.randomize();
$display ("After randomization var1=%0d var2=%0d", f.var1, f.var2);
end
endmodule
var2 stays at its pre-randomization value (0, unless something else set it earlier), while var1 is free to take any value from 0 to 15 — the mirror image of the earlier example.
Summary
In this tutorial, you learned:
rand_mode()enables or disables randomization for a single variable, or for everyrand/randcproperty in an object at once- Called with no argument,
rand_mode()returns the current state as1(enabled) or0(disabled) - A disabled variable is skipped by the solver and keeps whatever value it already had
rand_mode()state is tracked per object instance, not per classrand_mode()disables a variable;constraint_mode()disables a constraint block — they are not interchangeable
Continuing the Randomization Chapter
Continue with randcase to see the same enable/disable-style control applied to randomizing control flow itself, or see Disable Constraints for the constraint-block counterpart to rand_mode().