randcase vs randsequence in SystemVerilog: one weighted pick, or a whole random sequence. Tested on VCS. The Art of Verification

randcase vs randsequence in SystemVerilog, With Examples (Tested)

In short: randcase picks one branch at random using weights, like a weighted case. randsequence builds a whole random sequence from named productions, with choices, weights, if, case and repeat. Use randcase for a single weighted decision and randsequence when the order of many steps matters.

Not every random decision needs a class and a constraint. Sometimes you only want “send an error packet about 10% of the time”, or “a write followed by two reads”.

In this lesson we start with two tiny snippets, then go further with a UVM bench of 8 checks, including a weight bug that makes an error test pass while testing nothing. I ran all of it on Synopsys VCS.

Start here: the simplest example

One randcase with three weights, run 1000 times, and one randsequence that runs three steps in order.

// tav_rcase_simple.sv : the smallest randcase and randsequence 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), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".

`include "uvm_macros.svh"

module tav_top;
  import uvm_pkg::*;
  int    tav_c1, tav_c2, tav_c3;
  string tav_s;

  initial begin
    // randcase: pick ONE branch, using the weights
    repeat (1000) begin
      randcase
        7 : tav_c1++;     // about 7 out of 15
        3 : tav_c2++;     // about 3 out of 15
        5 : tav_c3++;     // about 5 out of 15
      endcase
    end
    `uvm_info("TAV_SIMPLE", $sformatf("randcase x1000: c1=%0d c2=%0d c3=%0d (expect about 467 / 200 / 333)",
              tav_c1, tav_c2, tav_c3), UVM_NONE)

    // randsequence: run productions, here simply in order
    randsequence (main)
      main  : one two three ;
      one   : { tav_s = {tav_s, "one "};   };
      two   : { tav_s = {tav_s, "two "};   };
      three : { tav_s = {tav_s, "three"};  };
    endsequence
    `uvm_info("TAV_SIMPLE", {"randsequence: ", tav_s}, UVM_NONE)
  end
endmodule

Here is what VCS printed:

UVM_INFO testbench.sv(22) @ 0: reporter [TAV_SIMPLE] randcase x1000: c1=468 c2=220 c3=312 (expect about 467 / 200 / 333)
UVM_INFO testbench.sv(32) @ 0: reporter [TAV_SIMPLE] randsequence: one two three
  • Line 1: 468, 220 and 312 are close to the expected 467, 200 and 333. Your numbers will differ with a different seed, but the split stays near 7 : 3 : 5.
  • Line 2: main : one two three ran the three productions in order.

That is the core of both: randcase makes one weighted pick, and randsequence runs a list of steps. The full bench below adds the parts you need in real tests.

What you will learn

  • How randcase weights work, including zero weights.
  • How to tune an error-injection rate with weight variables.
  • How randsequence productions, choices and := weights work.
  • How to use if, case and repeat inside a randsequence.
  • When to use each one, and when a constrained class is the better tool.

Cheat sheet: randcase vs randsequence

randcaserandsequence
What it doesRuns one branch, picked by weightRuns a tree of productions
Weights7 : stmt;a := 7 | b := 3
Order of stepsOne step onlySequences, choices, repeat, if, case
Weight of 0Never pickedNever picked
Typical useError injection rate, picking a packet typeTraffic patterns, protocol scenarios
Where it livesAny procedural codeAny procedural code

Analogy: randcase is rolling one loaded die. randsequence is a recipe where some steps say “add one of these, at random”.

Diagram: on the left, randcase makes one weighted pick between tav_c1++ (weight 7, 468 of 1000), tav_c2++ (weight 3, 220 of 1000) and tav_c3++ (weight 5, 312 of 1000). On the right, a randsequence tree: main expands to wr and reads, reads to repeat (2) op, and op to case (tav_op) 1: rd, giving WR RD RD
Both from the bench, as VCS ran them: the weighted randcase counts and the check 8 production tree.

Going further: one bench, 8 checks

  • Checks 1 to 4 test randcase: a 7 : 3 split, an error-injection branch, a zero weight, and all weights zero.
  • Checks 5 to 8 test randsequence: steps in order, a weighted choice, if/else, and repeat with case.
  • Planted bug: +BUG=1 leaves the error-injection weight at 0.

Run it yourself: both files are on GitHub, with the expected output.

// tav_rcase_uvm.sv : randcase vs randsequence 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), 30 Nov 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_rcase_test   8 self-checks, each printed with `uvm_info and checked with `uvm_error
//     1-4  randcase:     weights, an error-injection branch, a zero weight, all weights zero
//     5-8  randsequence: in order, weighted choice, if/else, repeat and case
//
// Run options:
//   (none)   all 8 checks pass, 0 errors
//   +BUG=1   the error-injection weight is left at 0, so error packets are never picked (check 2 fails)

`include "uvm_macros.svh"

package tav_rcase_pkg;
  import uvm_pkg::*;
  `include "uvm_macros.svh"

  class tav_rcase_test extends uvm_test;
    `uvm_component_utils(tav_rcase_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_RCASE", $sformatf("PASS %s: %s", what, got), UVM_LOW)
      end
      else begin
        tav_fail++;
        `uvm_error("TAV_RCASE", $sformatf("FAIL %s: got %s, expected %s", what, got, exp))
      end
    endfunction

    task run_phase(uvm_phase phase);
      int    tav_a, tav_b, tav_good, tav_err, tav_never, tav_pick;
      int    tav_w_good = 9;
      int    tav_w_err  = (tav_bug == 1) ? 0 : 1;   // BUG: the error weight is never set
      int    tav_w_zero = 0;
      bit    tav_is_wr  = 1;
      int    tav_op     = 1;
      string tav_s;
      phase.raise_objection(this);

      // 1. Weights 7 : 3 over 10000 picks: the first branch should land near 7000
      repeat (10000) randcase
        7 : tav_a++;
        3 : tav_b++;
      endcase
      `uvm_info("TAV_RCASE", $sformatf("1  counts: 7-weight=%0d 3-weight=%0d", tav_a, tav_b), UVM_LOW)
      tav_check("1 7:3 split, first branch in 6500..7500",
                (tav_a >= 6500 && tav_a <= 7500) ? "in range" : $sformatf("out of range (%0d)", tav_a), "in range");

      // 2. Error injection: weights are expressions, so a test can tune them
      repeat (1000) randcase
        tav_w_good : tav_good++;
        tav_w_err  : tav_err++;
      endcase
      `uvm_info("TAV_RCASE", $sformatf("2  counts: good=%0d err=%0d", tav_good, tav_err), UVM_LOW)
      tav_check("2 error packets picked at least once in 1000", (tav_err > 0) ? "yes" : "no", "yes");

      // 3. A weight of 0 is never picked
      repeat (1000) randcase
        1 : ;
        0 : tav_never++;
      endcase
      tav_check("3 zero-weight branch count", $sformatf("%0d", tav_never), "0");

      // 4. All weights 0: no branch runs at all
      tav_pick = -1;
      randcase
        tav_w_zero : tav_pick = 1;
        tav_w_zero : tav_pick = 2;
      endcase
      tav_check("4 all weights zero, tav_pick", $sformatf("%0d", tav_pick), "-1");

      // 5. randsequence: productions in order
      tav_s = "";
      randsequence (main)
        main  : one two three ;
        one   : { tav_s = {tav_s, "one "};  };
        two   : { tav_s = {tav_s, "two "};  };
        three : { tav_s = {tav_s, "three"}; };
      endsequence
      tav_check("5 sequence in order", tav_s, "one two three");

      // 6. Weighted choice with | and := (a weight of 0 is never chosen)
      tav_a = 0; tav_b = 0;
      repeat (100) randsequence (main)
        main : pick_a := 1 | pick_b := 0 ;
        pick_a : { tav_a++; };
        pick_b : { tav_b++; };
      endsequence
      tav_check("6 choice counts a / b", $sformatf("a=%0d b=%0d", tav_a, tav_b), "a=100 b=0");

      // 7. if / else inside randsequence
      tav_s = "";
      randsequence (main)
        main : if (tav_is_wr) wr else rd ;
        wr   : { tav_s = "WR"; };
        rd   : { tav_s = "RD"; };
      endsequence
      tav_check("7 if (tav_is_wr) wr else rd", tav_s, "WR");

      // 8. repeat and case: build a small traffic pattern
      tav_s = "";
      randsequence (main)
        main  : wr reads ;
        reads : repeat (2) op ;
        op    : case (tav_op) 0 : idle; 1 : rd; endcase ;
        wr    : { tav_s = {tav_s, "WR "}; };
        rd    : { tav_s = {tav_s, "RD "}; };
        idle  : { tav_s = {tav_s, "IDLE "}; };
      endsequence
      tav_check("8 wr, then repeat(2) of case(tav_op)", tav_s, "WR RD RD ");

      `uvm_info("TAV_RCASE", $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_rcase_pkg::*;
  initial run_test("tav_rcase_test");
endmodule

How to run it

  1. Open EDA Playground and pick Synopsys VCS with UVM 1.2.
  2. Paste the file into testbench.sv and leave design.sv empty.
  3. Run it once as is, then add +BUG=1 to the run options and run again.

What VCS returned

#CheckResultWhat it proves
17 : 3 split over 10000 picks7065 / 2935Weights set the odds, within a range
2Error branch, weight 1 vs 9, over 1000900 / 100Weights can be variables a test tunes
3A weight of 00 picksZero means never
4All weights 0-1 (unchanged)No branch runs, and VCS warns
5Steps in orderone two threeA production list runs left to right
6pick_a := 1 | pick_b := 0, 100 timesa=100 b=0| chooses one, := sets its weight
7if (tav_is_wr) wr else rdWRConditions can steer a sequence
8wr, then repeat (2) of case (tav_op)WR RD RDBuild a traffic pattern from small parts

Result: 8 pass, 0 fail. Here is the log.

UVM_INFO @ 0: reporter [RNTST] Running test tav_rcase_test...
UVM_INFO testbench.sv(62) @ 0: uvm_test_top [TAV_RCASE] 1  counts: 7-weight=7065 3-weight=2935
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 1 7:3 split, first branch in 6500..7500: in range
UVM_INFO testbench.sv(71) @ 0: uvm_test_top [TAV_RCASE] 2  counts: good=900 err=100
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 2 error packets picked at least once in 1000: yes
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 3 zero-weight branch count: 0

Warning-[RDCE-ZTWW] Randcase zero total weight warning
testbench.sv, 83
  Total weight is equal to zero.
  Please make sure the total weight is larger than zero.

UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 4 all weights zero, tav_pick: -1
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 5 sequence in order: one two three
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 6 choice counts a / b: a=100 b=0
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 7 if (tav_is_wr) wr else rd: WR
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 8 wr, then repeat(2) of case(tav_op): WR RD RD 
UVM_INFO testbench.sv(129) @ 0: uvm_test_top [TAV_RCASE] 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 :   14
UVM_WARNING :    0
UVM_ERROR :    0
UVM_FATAL :    0
** Report counts by id
[RNTST]     1
[TAV_RCASE]    11
[TEST_DONE]     1
[UVM/RELNOTES]     1

Look at check 4. VCS printed Warning-[RDCE-ZTWW] Randcase zero total weight warning, yet the UVM summary says UVM_WARNING : 0. Simulator warnings are not UVM reports, so a regression script that only reads the UVM summary would never see it.

The planted bug: an error test with no errors

With +BUG=1, the error weight tav_w_err stays 0. The randcase still runs 1000 times without complaint, and every pick goes to the good branch.

UVM_INFO @ 0: reporter [RNTST] Running test tav_rcase_test...
UVM_INFO testbench.sv(62) @ 0: uvm_test_top [TAV_RCASE] 1  counts: 7-weight=7065 3-weight=2935
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 1 7:3 split, first branch in 6500..7500: in range
UVM_INFO testbench.sv(71) @ 0: uvm_test_top [TAV_RCASE] 2  counts: good=1000 err=0
UVM_ERROR testbench.sv(43) @ 0: uvm_test_top [TAV_RCASE] FAIL 2 error packets picked at least once in 1000: got no, expected yes
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 3 zero-weight branch count: 0

Warning-[RDCE-ZTWW] Randcase zero total weight warning
testbench.sv, 83
  Total weight is equal to zero.
  Please make sure the total weight is larger than zero.

UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 4 all weights zero, tav_pick: -1
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 5 sequence in order: one two three
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 6 choice counts a / b: a=100 b=0
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 7 if (tav_is_wr) wr else rd: WR
UVM_INFO testbench.sv(39) @ 0: uvm_test_top [TAV_RCASE] PASS 8 wr, then repeat(2) of case(tav_op): WR RD RD 
UVM_INFO testbench.sv(129) @ 0: uvm_test_top [TAV_RCASE] 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 :   13
UVM_WARNING :    0
UVM_ERROR :    1
UVM_FATAL :    0
** Report counts by id
[RNTST]     1
[TAV_RCASE]    11
[TEST_DONE]     1
[UVM/RELNOTES]     1

The counts line says good=1000 err=0. Without check 2, this run would look like a normal pass, while the “error injection” test never injected a single error.

The lesson: when a weight is a variable, check that every branch you care about was actually picked. Counting the branches costs one line and catches a test that silently tests nothing.

When to use which

You needUse
One weighted choice, right here in the coderandcase
A sequence of steps with random choices along the wayrandsequence
Random values with relations between fieldsA class with rand fields and constraints
Reusable stimulus in a UVM testbenchUVM sequences, which may use randcase or randsequence inside body()

Common mistakes to avoid

MistakeWhat you seeFix
A weight variable left at 0That branch never runs, and nothing fails (+BUG=1)Count the branches and check each one ran
All weights 0No branch runs, and only a simulator warningMake sure at least one weight is above 0
Expecting exact countsCounts change with the seedCheck a range, as check 1 does
Forgetting the ; at the end of a productionCompile errorEvery production rule ends with ;
Using randsequence for field valuesHard-to-read codeUse constraints for values, randsequence for order

How AI helps you master this

  • Sketch a scenario: describe a protocol flow, like “write, then 1 to 3 reads, then maybe an error”, and ask for a randsequence.
  • Check the odds: paste a randcase and ask for the expected share of each branch. Then compare with your counts.
  • Find silent branches: paste your stimulus code and ask which weights could end up 0.

Warning: a convincing AI answer is not the same as a correct engineering result. AI tools often mix up randsequence syntax, so compile and run every snippet.

Interview questions on randcase and randsequence

  • What is the difference between randcase and randsequence? randcase picks one weighted branch. randsequence builds a sequence from productions.
  • How are randcase weights used? Each branch’s chance is its weight divided by the total of all weights.
  • What happens if all randcase weights are 0? No branch runs, and the simulator may warn, as VCS did.
  • How do you give weights to randsequence choices? With :=, as in a := 7 | b := 3.
  • Can weights be variables? Yes. Any integral expression works, which lets a test change the odds.

More practice: SystemVerilog interview questions.

Practice challenge

  1. Run the simple example twice with different seeds and compare the counts.
  2. Run the full bench, then +BUG=1, and find the counts line that gives the bug away.
  3. Change check 8 so reads picks rd := 3 | idle := 1 instead of using case. What patterns can you get?
  4. Bonus: make the number of reads random, from 1 to 3, using repeat ($urandom_range(1, 3)).

FAQ

What is the difference between randcase and randsequence? randcase runs one branch picked by weight. randsequence runs a whole set of productions, with random choices inside.

How are randcase weights used? A branch’s chance is its weight divided by the sum of all weights, so weights 7 and 3 give 70% and 30%.

What are terminals and non-terminals in randsequence? A terminal is a production with a code block, like wr : { ... };, and a non-terminal is made of other productions, like main : wr reads ;.

How do I make random choices in randsequence? Separate the options with |, and add := weights if they should not be equally likely.

Can randsequence do conditions and repeats? Yes. It supports if/else, case and repeat, as checks 7 and 8 show.

When should I use randcase over randsequence? When you need one weighted decision. Use randsequence when the order of several steps matters.

References

  • IEEE 1800-2017, SystemVerilog standard: section 18.16 (randcase) and section 18.17 (randsequence), including weights, if, case and repeat production statements.

Keep learning