rand_mode vs constraint_mode in SystemVerilog: Freeze a Variable or Drop a Rule (Tested)
In short: rand_mode(0) freezes a variable: randomize() leaves its value alone, but every constraint still has to hold. constraint_mode(0) switches off a named constraint: the variables stay random, and that one rule no longer applies. Call either with no argument to read its state, and pass 1 to switch it back on.
Most tests reuse one transaction class and change only a little: fix the address, allow an illegal length, or skip one rule for an error test. These two switches do exactly that, without writing a new class.
In this lesson we start with a tiny packet, then go further with a UVM bench of 8 checks, including the trap where a frozen variable breaks a rule. I ran all of it on Synopsys VCS, and its solver messages turned out to be the best explanation of all.
Start here: the simplest example
A packet with a random addr and len, and one rule: len must be 1 to 8. First we freeze addr, then we switch off the rule.
// tav_rmode_simple.sv : the smallest rand_mode / constraint_mode example (UVM 1.2 reporting only)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 1 Dec 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
`include "uvm_macros.svh"
import uvm_pkg::*;
class tav_packet;
rand bit [7:0] addr;
rand bit [3:0] len;
constraint c_len { len inside {[1:8]}; }
endclass
module tav_top;
tav_packet p;
initial begin
p = new();
p.addr = 8'hA5;
p.addr.rand_mode(0); // freeze addr: it keeps 'hA5
repeat (3) begin
void'(p.randomize());
`uvm_info("TAV_SIMPLE", $sformatf("rand_mode(0) on addr : addr=%h len=%0d", p.addr, p.len), UVM_NONE)
end
p.c_len.constraint_mode(0); // drop the rule: len may now be 9..15
void'(p.randomize() with { len == 12; });
`uvm_info("TAV_SIMPLE", $sformatf("c_len off, len == 12 : addr=%h len=%0d", p.addr, p.len), UVM_NONE)
end
endmodule
Here is what VCS printed:
UVM_INFO testbench.sv(24) @ 0: reporter [TAV_SIMPLE] rand_mode(0) on addr : addr=a5 len=2
UVM_INFO testbench.sv(24) @ 0: reporter [TAV_SIMPLE] rand_mode(0) on addr : addr=a5 len=2
UVM_INFO testbench.sv(24) @ 0: reporter [TAV_SIMPLE] rand_mode(0) on addr : addr=a5 len=5
UVM_INFO testbench.sv(29) @ 0: reporter [TAV_SIMPLE] c_len off, len == 12 : addr=a5 len=12
- Lines 1 to 3:
addrstayeda5every time, whilelenkept changing. Only the frozen variable stopped. - Line 4: with
c_lenoff,len == 12worked, even though 12 is outside 1 to 8.
One switch works on a variable, the other on a rule. The full bench below shows what happens when they meet.
What you will learn
- What
rand_mode()andconstraint_mode()each switch off. - How to read the current state of either switch.
- Why a frozen variable can make
randomize()fail. - How
randomize(var)picks which variables are random for one call. - How an error test uses
constraint_mode(0)in UVM.
Cheat sheet: rand_mode vs constraint_mode
rand_mode() | constraint_mode() | |
|---|---|---|
| Works on | A rand variable, or the whole object | A named constraint, or all of an object’s constraints |
(0) means | Freeze: keep the current value | Ignore this rule |
| Other rules | Still apply to the frozen value | Still apply |
| Read the state | p.addr.rand_mode() | p.c_len.constraint_mode() |
| Typical use | Fix one field for a directed test | Allow illegal values for an error test |
Analogy: think of a board game. rand_mode(0) glues one piece to the board, but the rules still apply to it. constraint_mode(0) tears one rule out of the rulebook, and every piece can move freely again.
Going further: one bench, 8 checks
tav_packethasrand addrandlen, withc_len(1 to 8) andc_addr(addr >= 'h10).tav_axi_itemis a UVM transaction with a legal-length rule, for an error test.- Checks 3 and 5 make
randomize()fail on purpose, so VCS prints solver messages there. - Planted bug:
+BUG=1useslen.rand_mode(0)where check 3 neededc_len.constraint_mode(0).
Run it yourself: both files are on GitHub, with the expected output.
// tav_rmode_uvm.sv : rand_mode vs constraint_mode in SystemVerilog, a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 1 Dec 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// What is in here:
// tav_packet rand addr and len, constraints c_len (1..8) and c_addr (addr >= 'h10)
// tav_axi_item a UVM transaction with a legal-length rule, for an error test
// tav_rmode_test 8 self-checks, each printed with `uvm_info and checked with `uvm_error
//
// Note: checks 3 and 5 make randomize() fail ON PURPOSE, so VCS prints constraint-solver messages there.
//
// Run options:
// (none) all 8 checks pass, 0 errors
// +BUG=1 check 3 uses len.rand_mode(0) where it meant c_len.constraint_mode(0), so check 3 fails
`include "uvm_macros.svh"
package tav_rmode_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
class tav_packet;
rand bit [7:0] addr;
rand bit [3:0] len;
constraint c_len { len inside {[1:8]}; }
constraint c_addr { addr >= 8'h10; }
endclass
class tav_axi_item extends uvm_sequence_item;
`uvm_object_utils(tav_axi_item)
rand bit [7:0] tav_len;
constraint c_legal_len { tav_len inside {[1:16]}; }
function new(string name = "tav_axi_item");
super.new(name);
endfunction
endclass
class tav_rmode_test extends uvm_test;
`uvm_component_utils(tav_rmode_test)
int tav_pass, tav_fail, tav_bug;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
void'($value$plusargs("BUG=%d", tav_bug));
endfunction
function void tav_check(string what, string got, string exp);
if (got == exp) begin
tav_pass++;
`uvm_info("TAV_RMODE", $sformatf("PASS %s: %s", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_RMODE", $sformatf("FAIL %s: got %s, expected %s", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
tav_packet p;
tav_axi_item it;
string s;
int ok_on, ok_off;
phase.raise_objection(this);
// 1. rand_mode(0) freezes one variable: addr keeps its value over 3 randomize() calls
p = new();
p.addr = 8'hA5;
p.addr.rand_mode(0);
s = "";
repeat (3) begin
void'(p.randomize());
s = {s, $sformatf("%h ", p.addr)};
end
tav_check("1 addr after 3 randomize() with addr.rand_mode(0)", s, "a5 a5 a5 ");
// 2. rand_mode() with no argument reads the state: 0 = frozen, 1 = random
tav_check("2 addr.rand_mode() / len.rand_mode()",
$sformatf("%0d / %0d", p.addr.rand_mode(), p.len.rand_mode()), "0 / 1");
// 3. constraint_mode(0) drops one rule: len == 12 fails with c_len on, works with it off
p = new();
ok_on = p.randomize() with { len == 12; }; // fails on purpose: c_len says 1..8
if (tav_bug == 1)
p.len.rand_mode(0); // BUG: freezes len, the rule stays on
else
p.c_len.constraint_mode(0);
ok_off = p.randomize() with { len == 12; };
tav_check("3 len == 12 with c_len on / off, then len",
$sformatf("%0d / %0d, len=%0d", ok_on, ok_off, p.len), "0 / 1, len=12");
// 4. constraint_mode() with no argument reads the state (its own object, so check 3 does not affect it)
p = new();
p.c_len.constraint_mode(0);
tav_check("4 c_len.constraint_mode() / c_addr.constraint_mode()",
$sformatf("%0d / %0d", p.c_len.constraint_mode(), p.c_addr.constraint_mode()), "0 / 1");
// 5. The trap: a frozen variable must still obey the rules
p = new();
p.addr = 8'h05; // breaks c_addr (addr >= 'h10)
p.addr.rand_mode(0);
tav_check("5 randomize() with frozen addr='h05 and c_addr on", $sformatf("%0d", p.randomize()), "0");
// 6. rand_mode(0) on the whole object: nothing changes, but the rules are still checked
p = new();
p.addr = 8'h20; p.len = 3;
p.rand_mode(0);
tav_check("6 p.rand_mode(0): randomize() / addr / len",
$sformatf("%0d / %h / %0d", p.randomize(), p.addr, p.len), "1 / 20 / 3");
// 7. randomize(len): only len is randomized this time, addr stays even though it is rand
p = new();
p.addr = 8'h77;
tav_check("7 randomize(len): result / addr / len in 1..8",
$sformatf("%0d / %h / %0d", p.randomize(len), p.addr, (p.len inside {[1:8]})), "1 / 77 / 1");
// 8. UVM error test: switch off the legal-length rule to send a zero-length item
it = tav_axi_item::type_id::create("it");
it.c_legal_len.constraint_mode(0);
tav_check("8 error item: randomize() with tav_len == 0",
$sformatf("%0d / len=%0d", it.randomize() with { tav_len == 0; }, it.tav_len), "1 / len=0");
`uvm_info("TAV_RMODE", $sformatf("Checks: %0d pass, %0d fail", tav_pass, tav_fail), UVM_NONE)
phase.drop_objection(this);
endtask
endclass
endpackage
module tav_top;
import uvm_pkg::*;
import tav_rmode_pkg::*;
initial run_test("tav_rmode_test");
endmodule
How to run it
- Open EDA Playground and pick Synopsys VCS with UVM 1.2.
- Paste the file into
testbench.svand leavedesign.svempty. - Run it once as is, then add
+BUG=1to the run options and run again.
What VCS returned
| # | Check | Result | What it proves |
|---|---|---|---|
| 1 | addr after 3 randomize(), frozen | a5 a5 a5 | A frozen variable keeps its value |
| 2 | addr.rand_mode() / len.rand_mode() | 0 / 1 | No argument reads the state |
| 3 | len == 12 with c_len on / off | 0 / 1, len=12 | Dropping the rule makes 12 legal |
| 4 | c_len.constraint_mode() / c_addr.constraint_mode() | 0 / 1 | Each rule has its own switch |
| 5 | Frozen addr='h05 with c_addr on | 0 | The trap: a frozen value must still obey the rules |
| 6 | p.rand_mode(0) on the object | 1 / 20 / 3 | Nothing changes, but the rules are still checked |
| 7 | randomize(len) | 1 / 77 / 1 | Only the named variable is random for that call |
| 8 | UVM item, c_legal_len off, tav_len == 0 | 1 / len=0 | An error test can send an illegal length |
Result: 8 pass, 0 fail. Here is the full log, solver messages included.
UVM_INFO @ 0: reporter [RNTST] Running test tav_rmode_test...
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 1 addr after 3 randomize() with addr.rand_mode(0): a5 a5 a5
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 2 addr.rand_mode() / len.rand_mode(): 0 / 1
=======================================================
Solver failed when solving following set of constraints
rand bit[3:0] len; // rand_mode = ON
constraint c_len // (from this) (constraint_mode = ON) (testbench.sv:27)
{
(len inside {[1:8]});
}
constraint WITH_CONSTRAINT // (from this) (constraint_mode = ON) (testbench.sv:89)
{
(len == 4'hc);
}
=======================================================
Note-[CNST-SATE] Standalone test extracted
A standalone test-case for this failure has automatically been extracted
from randomize serial 4 partition 1.
To reproduce the error using the extracted testcase, please use the
following command:
cd /home/runner/./simv.cst/testcases;
vcs -sverilog extracted_r_4_p_1_inconsistent_constraints.sv -R
To reproduce the error using the original design and verbose logging, re-run
simulation using:
simv +ntb_solver_debug=trace +ntb_solver_debug_filter=4
To reproduce the error using the original design and debug the error with
Verdi/DVE:
1. re-compile the original design with -debug_access+all, if not already
done so
% vcs -debug_access+all <other options>
2. re-run the simulation interactively with -gui/-verdi
% simv -gui/-verdi <other options>
3. enter the following commands to begin interactive constraint
inconsistency debug within Verdi/DVE
I. set the breakpoint: verdi/dve> stop -solver -serial 4
II. run the simulation till it stops: verdi/dve> run
III. step in the constraint solver: verdi/dve> step -solver
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 89
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 3 len == 12 with c_len on / off, then len: 0 / 1, len=12
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 4 c_len.constraint_mode() / c_addr.constraint_mode(): 0 / 1
=======================================================
Solver failed when solving following set of constraints
rand bit[7:0] addr = 8'h5; // rand_mode = OFF
constraint c_addr // (from this) (constraint_mode = ON) (testbench.sv:28)
{
(addr >= 8'h10);
}
=======================================================
Note-[CNST-SATE] Standalone test extracted
A standalone test-case for this failure has automatically been extracted
from randomize serial 6 partition 2.
To reproduce the error using the extracted testcase, please use the
following command:
cd /home/runner/./simv.cst/testcases;
vcs -sverilog extracted_r_6_p_2_inconsistent_constraints.sv -R
To reproduce the error using the original design and verbose logging, re-run
simulation using:
simv +ntb_solver_debug=trace +ntb_solver_debug_filter=6
To reproduce the error using the original design and debug the error with
Verdi/DVE:
1. re-compile the original design with -debug_access+all, if not already
done so
% vcs -debug_access+all <other options>
2. re-run the simulation interactively with -gui/-verdi
% simv -gui/-verdi <other options>
3. enter the following commands to begin interactive constraint
inconsistency debug within Verdi/DVE
I. set the breakpoint: verdi/dve> stop -solver -serial 6
II. run the simulation till it stops: verdi/dve> run
III. step in the constraint solver: verdi/dve> step -solver
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 108
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 5 randomize() with frozen addr='h05 and c_addr on: 0
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 6 p.rand_mode(0): randomize() / addr / len: 1 / 20 / 3
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 7 randomize(len): result / addr / len in 1..8: 1 / 77 / 1
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 8 error item: randomize() with tav_len == 0: 1 / len=0
UVM_INFO testbench.sv(129) @ 0: uvm_test_top [TAV_RMODE] Checks: 8 pass, 0 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 12
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_RMODE] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
Read the solver messages: they explain everything
When randomize() fails, VCS prints the exact set of variables and constraints that clash, with the state of both switches.
- Check 3:
rand bit[3:0] len; // rand_mode = ONagainstc_len ... (constraint_mode = ON)and the inlinelen == 4'hc. The rule and the request cannot both be true. - Check 5:
rand bit[7:0] addr = 8'h5; // rand_mode = OFFagainstc_addr. The variable is frozen at 5, the rule wants 16 or more, and nothing can give.
Also notice that the clean run ends with UVM_ERROR : 0, even though VCS printed two Error-[CNST-CIF] messages. Solver errors are simulator messages, not UVM reports, so always check the return value of randomize().
The planted bug: freezing instead of dropping
With +BUG=1, check 3 calls p.len.rand_mode(0), which is a very common mix-up. The goal was to allow len == 12.
UVM_INFO @ 0: reporter [RNTST] Running test tav_rmode_test...
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 1 addr after 3 randomize() with addr.rand_mode(0): a5 a5 a5
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 2 addr.rand_mode() / len.rand_mode(): 0 / 1
=======================================================
Solver failed when solving following set of constraints
rand bit[3:0] len; // rand_mode = ON
constraint c_len // (from this) (constraint_mode = ON) (testbench.sv:27)
{
(len inside {[1:8]});
}
constraint WITH_CONSTRAINT // (from this) (constraint_mode = ON) (testbench.sv:89)
{
(len == 4'hc);
}
=======================================================
Note-[CNST-SATE] Standalone test extracted
A standalone test-case for this failure has automatically been extracted
from randomize serial 4 partition 1.
To reproduce the error using the extracted testcase, please use the
following command:
cd /home/runner/./simv.cst/testcases;
vcs -sverilog extracted_r_4_p_1_inconsistent_constraints.sv -R
To reproduce the error using the original design and verbose logging, re-run
simulation using:
simv +ntb_solver_debug=trace +ntb_solver_debug_filter=4
To reproduce the error using the original design and debug the error with
Verdi/DVE:
1. re-compile the original design with -debug_access+all, if not already
done so
% vcs -debug_access+all <other options>
2. re-run the simulation interactively with -gui/-verdi
% simv -gui/-verdi <other options>
3. enter the following commands to begin interactive constraint
inconsistency debug within Verdi/DVE
I. set the breakpoint: verdi/dve> stop -solver -serial 4
II. run the simulation till it stops: verdi/dve> run
III. step in the constraint solver: verdi/dve> step -solver
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 89
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
=======================================================
Solver failed when solving following set of constraints
rand bit[3:0] len = 4'h0; // rand_mode = OFF
constraint WITH_CONSTRAINT // (from this) (constraint_mode = ON) (testbench.sv:94)
{
(len == 4'hc);
}
=======================================================
Note-[CNST-SATE] Standalone test extracted
A standalone test-case for this failure has automatically been extracted
from randomize serial 5 partition 1.
To reproduce the error using the extracted testcase, please use the
following command:
cd /home/runner/./simv.cst/testcases;
vcs -sverilog extracted_r_5_p_1_inconsistent_constraints.sv -R
To reproduce the error using the original design and verbose logging, re-run
simulation using:
simv +ntb_solver_debug=trace +ntb_solver_debug_filter=5
To reproduce the error using the original design and debug the error with
Verdi/DVE:
1. re-compile the original design with -debug_access+all, if not already
done so
% vcs -debug_access+all <other options>
2. re-run the simulation interactively with -gui/-verdi
% simv -gui/-verdi <other options>
3. enter the following commands to begin interactive constraint
inconsistency debug within Verdi/DVE
I. set the breakpoint: verdi/dve> stop -solver -serial 5
II. run the simulation till it stops: verdi/dve> run
III. step in the constraint solver: verdi/dve> step -solver
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 94
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
UVM_ERROR testbench.sv(61) @ 0: uvm_test_top [TAV_RMODE] FAIL 3 len == 12 with c_len on / off, then len: got 0 / 0, len=0, expected 0 / 1, len=12
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 4 c_len.constraint_mode() / c_addr.constraint_mode(): 0 / 1
=======================================================
Solver failed when solving following set of constraints
rand bit[7:0] addr = 8'h5; // rand_mode = OFF
constraint c_addr // (from this) (constraint_mode = ON) (testbench.sv:28)
{
(addr >= 8'h10);
}
=======================================================
Note-[CNST-SATE] Standalone test extracted
A standalone test-case for this failure has automatically been extracted
from randomize serial 6 partition 2.
To reproduce the error using the extracted testcase, please use the
following command:
cd /home/runner/./simv.cst/testcases;
vcs -sverilog extracted_r_6_p_2_inconsistent_constraints.sv -R
To reproduce the error using the original design and verbose logging, re-run
simulation using:
simv +ntb_solver_debug=trace +ntb_solver_debug_filter=6
To reproduce the error using the original design and debug the error with
Verdi/DVE:
1. re-compile the original design with -debug_access+all, if not already
done so
% vcs -debug_access+all <other options>
2. re-run the simulation interactively with -gui/-verdi
% simv -gui/-verdi <other options>
3. enter the following commands to begin interactive constraint
inconsistency debug within Verdi/DVE
I. set the breakpoint: verdi/dve> stop -solver -serial 6
II. run the simulation till it stops: verdi/dve> run
III. step in the constraint solver: verdi/dve> step -solver
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 108
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 5 randomize() with frozen addr='h05 and c_addr on: 0
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 6 p.rand_mode(0): randomize() / addr / len: 1 / 20 / 3
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 7 randomize(len): result / addr / len in 1..8: 1 / 77 / 1
UVM_INFO testbench.sv(57) @ 0: uvm_test_top [TAV_RMODE] PASS 8 error item: randomize() with tav_len == 0: 1 / len=0
UVM_INFO testbench.sv(129) @ 0: uvm_test_top [TAV_RMODE] Checks: 7 pass, 1 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 11
UVM_WARNING : 0
UVM_ERROR : 1
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_RMODE] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
The second solver message says it all: rand bit[3:0] len = 4'h0; // rand_mode = OFF. Instead of dropping the rule, rand_mode(0) froze len at 0, so len == 12 was impossible and check 3 failed with got 0 / 0, len=0.
The lesson: to allow values a rule forbids, switch off the rule with constraint_mode(0). rand_mode(0) only stops a variable from changing, and every rule still applies to it.
A third switch: randomize() with arguments
p.randomize(len) randomizes only len for that one call. Every other variable is treated as fixed, even if it is declared rand, as check 7 shows with addr staying 77. Nothing stays switched off afterwards, so it is handy for one-off calls.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
Using rand_mode(0) to allow illegal values | The rule still applies, randomize() fails (+BUG=1) | Use constraint_mode(0) on the rule |
| Freezing a value that breaks a rule | randomize() returns 0, solver error (check 5) | Set a legal value, or switch off that rule too |
Not checking the return of randomize() | Old values are used, and UVM_ERROR stays 0 | if (!p.randomize()) `uvm_error(...) |
| Forgetting to switch back on | Later tests on the same object stay unconstrained | Call constraint_mode(1), or use a fresh object |
Switching off a static constraint for one object | It goes off for every object of the class | Use a non-static constraint for per-object control |
How AI helps you master this
- Read a solver dump: paste a VCS
Solver failedblock and ask which switch or value causes the clash. - Plan an error test: describe the illegal value you need and ask which constraint to switch off.
- Review a test: paste it and ask where
rand_modeis used whenconstraint_modewas meant.
Warning: a convincing AI answer is not the same as a correct engineering result. AI tools sometimes say rand_mode(0) removes a variable from the constraints, and the bug run shows it does not. Run the code.
Interview questions on rand_mode and constraint_mode
- What is the difference between
rand_modeandconstraint_mode?rand_modefreezes a variable.constraint_modeswitches off a rule. - Why can
randomize()fail afterrand_mode(0)? The frozen value must still satisfy every active constraint. - How do you check if a constraint is on? Call
constraint_mode()with no argument: 1 is on, 0 is off. - Does
p.rand_mode(0)switch off the constraints? No. It freezes every variable, andrandomize()then only checks the rules. - How do you randomize just one variable once? Pass it as an argument:
p.randomize(len).
More practice: SystemVerilog interview questions.
Practice challenge
- Run the simple example and confirm
addrnever changes. - Run the full bench, then
+BUG=1, and findrand_mode = OFFin the solver message. - In check 5, switch off
c_addras well. Predict the return value ofrandomize(), then run. - Bonus: in check 6, set
len = 12beforerandomize(). What happens, and why?
FAQ
What is the difference between rand_mode and constraint_mode? rand_mode(0) stops a variable from being randomized. constraint_mode(0) stops a constraint from being applied.
Why does randomize() fail after rand_mode(0)? The frozen value is still checked against every active constraint. If it breaks one, the solver cannot succeed.
How do I disable randomization for one variable only? Call rand_mode(0) on that variable, as in p.addr.rand_mode(0).
How do I check if a constraint is on? Call p.c_len.constraint_mode() with no argument. It returns 1 when the constraint is on.
Does rand_mode(0) on the object switch off constraints? No. All variables are frozen, and randomize() only checks that the current values meet the rules.
Do I need to switch modes back on? Yes, if you reuse the object. The switches stay as you left them until you change them.
References
- IEEE 1800-2017, SystemVerilog standard: section 18.8 (
rand_mode()), section 18.9 (constraint_mode()), section 18.11 (in-line random variable control,randomize()with arguments) and section 18.5.11 (static constraints).
Keep learning
- More randomization: unique values without randomize and randcase vs randsequence.
- Classes behind it: inheritance, where a child class can replace a constraint by name.
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification
