Unique values in SystemVerilog without randomize(): fill and shuffle, formulas, and pick until unused. Tested on VCS. The Art of Verification

Unique Values in SystemVerilog Without randomize(): shuffle and More (Tested)

In short: To get an array of unique values without randomize(), fill it with values you know are different, for example data[i] = i, then call data.shuffle(). Shuffle changes the order but never the values, so nothing can repeat. If the values must come from a wider range, pick with $urandom_range and remember what you already used, or take values from a shuffled pool.

You often need a list with no repeats: transaction IDs, addresses for a memory test, a random order of channels. A unique constraint works, but sometimes you are not in a class, or you want something faster and simpler.

In this lesson we start with fill and shuffle, then go further with a UVM bench of 8 checks, including two ways that quietly produce repeats. I ran all of it on Synopsys VCS.

Start here: the simplest example

Fill 10 slots with 0 to 9, shuffle them, then sort a copy back to prove that no value was lost or repeated.

// tav_uniq_simple.sv : the smallest "unique values without randomize" 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"

module tav_top;
  import uvm_pkg::*;
  int tav_data[10];

  initial begin
    foreach (tav_data[i]) tav_data[i] = i;   // 1. fill: 0..9, every value once
    `uvm_info("TAV_SIMPLE", $sformatf("after fill    : %p", tav_data), UVM_NONE)

    tav_data.shuffle();                      // 2. shuffle: new order, same values
    `uvm_info("TAV_SIMPLE", $sformatf("after shuffle : %p", tav_data), UVM_NONE)

    tav_data.sort();                         // 3. sort a check: still 0..9, so no duplicates
    `uvm_info("TAV_SIMPLE", $sformatf("sorted again  : %p", tav_data), UVM_NONE)
  end
endmodule

Here is what VCS printed:

UVM_INFO testbench.sv(14) @ 0: reporter [TAV_SIMPLE] after fill    : '{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 
UVM_INFO testbench.sv(17) @ 0: reporter [TAV_SIMPLE] after shuffle : '{8, 6, 7, 9, 5, 0, 1, 4, 2, 3} 
UVM_INFO testbench.sv(20) @ 0: reporter [TAV_SIMPLE] sorted again  : '{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  • Line 2: the same 10 values, in a new order. Your order will differ with a different seed.
  • Line 3: sorted back, it is 0 to 9 again. Shuffle moved values around but never created or removed one.

That is the whole trick. The full bench below adds formulas, wider ranges and the mistakes to avoid.

What you will learn

  • Why fill and shuffle can never produce a repeat.
  • How to fill with a formula, and when a formula breaks uniqueness.
  • How to pick unique values from a wider range with $urandom_range.
  • How to take unique values one at a time from a shuffled pool.
  • How this compares with the unique constraint.

Cheat sheet: four ways to get unique values

MethodCode ideaBest for
Fill and shuffled[i] = i; d.shuffle();A random order of a known set
Formula and shufflea[i] = 'h100 + i*4; a.shuffle();Aligned addresses, IDs with a step
Pick until unused$urandom_range plus a used[int] tableA few values from a big range
Shuffled poolpool.shuffle(); v = pool.pop_front();Handing out values on demand
unique constraintconstraint c { unique {arr}; }Inside a class, with other constraints

Analogy: fill and shuffle is shuffling a deck of cards. You get a random order, but you can never draw two aces of spades, because the deck only has one.

Diagram: fill gives 0 to 9 in order, shuffle gives the same 10 values in a new order (8, 6, 7, 9, 5, 0, 1, 4, 2, 3), and a sort check gives 0 to 9 again. Below, two wrong ways: filling with i/2 gives pairs and only 5 different values, and picking with urandom_range without marking used values gave 3, 8, 7, 7, 8, 2, 3, 6, 6, 9, only 6 different values
What VCS returned in the bench: the right way, and two ways that repeat values.

Going further: one bench, 8 checks

  • Checks 1 to 3: fill and shuffle, then prove the set and size did not change.
  • Checks 4 and 5: a good formula (aligned addresses) and a bad one (i/2).
  • Check 6: pick until unused, with $urandom_range and an associative array.
  • Check 7: a shuffled pool that hands out values one at a time.
  • Check 8: the unique constraint, for comparison.
  • Planted bug: +BUG=1 makes check 6 forget to mark values as used.

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

// tav_uniq_uvm.sv : unique values in SystemVerilog without randomize(), 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_uniq_item    the "fully random" alternative: rand array + unique constraint (for check 8)
//   tav_uniq_test    8 self-checks, each printed with `uvm_info and checked with `uvm_error
//     1-3  fill and shuffle      4-5  fill with a formula (good and bad)
//     6    pick until unused     7    shuffled pool, pop values on demand     8  unique constraint
//
// Run options:
//   (none)   all 8 checks pass, 0 errors
//   +BUG=1   the "pick until unused" loop forgets to mark a value as used, so duplicates appear (check 6 fails)

`include "uvm_macros.svh"

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

  // The fully random alternative, for comparison
  class tav_uniq_item extends uvm_sequence_item;
    `uvm_object_utils(tav_uniq_item)
    rand int tav_arr[10];
    constraint c_range  { foreach (tav_arr[i]) tav_arr[i] inside {[0:99]}; }
    constraint c_unique { unique {tav_arr}; }
    function new(string name = "tav_uniq_item");
      super.new(name);
    endfunction
  endclass

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

    // How many different values does a queue hold?
    function int tav_count_unique(int q[$]);
      bit seen[int];
      foreach (q[i]) seen[q[i]] = 1;
      return seen.num();
    endfunction

    task run_phase(uvm_phase phase);
      int tav_data[10], tav_copy[10], tav_addr[8], tav_bad[10];
      int tav_ref[10] = '{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};   // an assignment pattern is legal here, not inside ==
      int tav_q[$], tav_pool[$], tav_got[$];
      bit tav_used[int];
      bit tav_ok;
      int v;
      tav_uniq_item it;
      phase.raise_objection(this);

      // 1-3. Fill and shuffle
      foreach (tav_data[i]) tav_data[i] = i;
      tav_data.shuffle();
      `uvm_info("TAV_UNIQ", $sformatf("1  shuffled: %p", tav_data), UVM_LOW)
      tav_copy = tav_data;
      tav_copy.sort();
      // compare the arrays themselves, not their printed text (%p formatting differs between tools)
      tav_check("1 sorted copy vs tav_ref", (tav_copy == tav_ref) ? "same" : "different", "same");
      tav_q = tav_data;
      tav_check("2 different values after shuffle", $sformatf("%0d", tav_count_unique(tav_q)), "10");
      tav_check("3 size after shuffle", $sformatf("%0d", $size(tav_data)), "10");

      // 4. Fill with a formula: 4-byte aligned addresses from 'h100, then shuffle
      foreach (tav_addr[i]) tav_addr[i] = 'h100 + i * 4;
      tav_addr.shuffle();
      `uvm_info("TAV_UNIQ", $sformatf("4  addresses: %p", tav_addr), UVM_LOW)
      tav_q = tav_addr;
      tav_ok = 1;
      foreach (tav_addr[i]) if (tav_addr[i] % 4 != 0) tav_ok = 0;
      tav_check("4 addresses: different / all aligned", $sformatf("%0d / %0d", tav_count_unique(tav_q), tav_ok), "8 / 1");

      // 5. A formula that is not one-to-one gives duplicates: i/2 makes pairs
      foreach (tav_bad[i]) tav_bad[i] = i / 2;
      tav_q = tav_bad;
      tav_check("5 fill with i/2: different values out of 10", $sformatf("%0d", tav_count_unique(tav_q)), "5");

      // 6. Pick until unused: $urandom_range, remember what was already picked
      tav_got.delete();
      while (tav_got.size() < 10) begin
        v = $urandom_range(0, 9);
        if (tav_bug == 1) tav_got.push_back(v);                 // BUG: never checks or marks tav_used
        else if (!tav_used.exists(v)) begin
          tav_used[v] = 1;
          tav_got.push_back(v);
        end
      end
      `uvm_info("TAV_UNIQ", $sformatf("6  picked: %p", tav_got), UVM_LOW)
      tav_check("6 pick until unused: different values out of 10", $sformatf("%0d", tav_count_unique(tav_got)), "10");

      // 7. A shuffled pool: take values one at a time, on demand
      tav_pool.delete();
      for (int i = 0; i < 10; i++) tav_pool.push_back(i * 10);
      tav_pool.shuffle();
      tav_got.delete();
      repeat (3) tav_got.push_back(tav_pool.pop_front());
      `uvm_info("TAV_UNIQ", $sformatf("7  took %p, %0d left", tav_got, tav_pool.size()), UVM_LOW)
      tav_check("7 taken different / left in pool", $sformatf("%0d / %0d", tav_count_unique(tav_got), tav_pool.size()), "3 / 7");

      // 8. The fully random alternative: rand array + unique constraint
      it = tav_uniq_item::type_id::create("it");
      if (!it.randomize()) `uvm_error("TAV_UNIQ", "randomize() failed")
      `uvm_info("TAV_UNIQ", $sformatf("8  randomized: %p", it.tav_arr), UVM_LOW)
      tav_q = it.tav_arr;
      tav_check("8 unique constraint: different values out of 10", $sformatf("%0d", tav_count_unique(tav_q)), "10");

      `uvm_info("TAV_UNIQ", $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_uniq_pkg::*;
  initial run_test("tav_uniq_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
1Shuffled array, sorted copy vs 0..9sameShuffle keeps every value
2Different values after shuffle10No repeats
3Size after shuffle10Nothing added or removed
4'h100 + i*4: different / aligned8 / 1A one-to-one formula stays unique
5Fill with i/25 of 10A formula that is not one-to-one makes repeats
6Pick until unused, 0..910The used table blocks repeats
7Shuffled pool, take 33 / 7Each value is handed out once
8unique constraint, 0..9910The constraint solver gives the same guarantee

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

UVM_INFO @ 0: reporter [RNTST] Running test tav_uniq_test...
UVM_INFO testbench.sv(79) @ 0: uvm_test_top [TAV_UNIQ] 1  shuffled: '{5, 8, 3, 7, 0, 1, 9, 4, 2, 6} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 1 sorted copy vs tav_ref: same
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 2 different values after shuffle: 10
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 3 size after shuffle: 10
UVM_INFO testbench.sv(91) @ 0: uvm_test_top [TAV_UNIQ] 4  addresses: '{284, 280, 272, 260, 276, 264, 256, 268} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 4 addresses: different / all aligned: 8 / 1
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 5 fill with i/2: different values out of 10: 5
UVM_INFO testbench.sv(112) @ 0: uvm_test_top [TAV_UNIQ] 6  picked: '{3, 8, 7, 2, 6, 9, 0, 5, 1, 4} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 6 pick until unused: different values out of 10: 10
UVM_INFO testbench.sv(121) @ 0: uvm_test_top [TAV_UNIQ] 7  took '{10, 0, 90} , 7 left
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 7 taken different / left in pool: 3 / 7
UVM_INFO testbench.sv(127) @ 0: uvm_test_top [TAV_UNIQ] 8  randomized: '{78, 62, 76, 5, 96, 56, 59, 27, 35, 71} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 8 unique constraint: different values out of 10: 10
UVM_INFO testbench.sv(131) @ 0: uvm_test_top [TAV_UNIQ] 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 :   17
UVM_WARNING :    0
UVM_ERROR :    0
UVM_FATAL :    0
** Report counts by id
[RNTST]     1
[TAV_UNIQ]    14
[TEST_DONE]     1
[UVM/RELNOTES]     1

The planted bug: forgetting what you already picked

With +BUG=1, the pick loop pushes every value $urandom_range returns, without checking or updating the used table.

UVM_INFO @ 0: reporter [RNTST] Running test tav_uniq_test...
UVM_INFO testbench.sv(79) @ 0: uvm_test_top [TAV_UNIQ] 1  shuffled: '{5, 8, 3, 7, 0, 1, 9, 4, 2, 6} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 1 sorted copy vs tav_ref: same
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 2 different values after shuffle: 10
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 3 size after shuffle: 10
UVM_INFO testbench.sv(91) @ 0: uvm_test_top [TAV_UNIQ] 4  addresses: '{284, 280, 272, 260, 276, 264, 256, 268} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 4 addresses: different / all aligned: 8 / 1
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 5 fill with i/2: different values out of 10: 5
UVM_INFO testbench.sv(112) @ 0: uvm_test_top [TAV_UNIQ] 6  picked: '{3, 8, 7, 7, 8, 2, 3, 6, 6, 9} 
UVM_ERROR testbench.sv(55) @ 0: uvm_test_top [TAV_UNIQ] FAIL 6 pick until unused: different values out of 10: got 6, expected 10
UVM_INFO testbench.sv(121) @ 0: uvm_test_top [TAV_UNIQ] 7  took '{80, 10, 0} , 7 left
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 7 taken different / left in pool: 3 / 7
UVM_INFO testbench.sv(127) @ 0: uvm_test_top [TAV_UNIQ] 8  randomized: '{50, 89, 6, 2, 91, 14, 76, 74, 47, 90} 
UVM_INFO testbench.sv(51) @ 0: uvm_test_top [TAV_UNIQ] PASS 8 unique constraint: different values out of 10: 10
UVM_INFO testbench.sv(131) @ 0: uvm_test_top [TAV_UNIQ] 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 :   16
UVM_WARNING :    0
UVM_ERROR :    1
UVM_FATAL :    0
** Report counts by id
[RNTST]     1
[TAV_UNIQ]    14
[TEST_DONE]     1
[UVM/RELNOTES]     1

The picked list is '{3, 8, 7, 7, 8, 2, 3, 6, 6, 9}: 7, 8, 3 and 6 each appear twice, so only 6 of the 10 values are different. The loop still ran 10 times and the array still has 10 entries, so only a uniqueness check notices.

The lesson: whenever you need “no repeats”, count the different values, as tav_count_unique() does. Checking the size is not enough.

Three traps when you check the result

Writing check 1 took three tries on VCS, and each failure is a trap worth knowing:

  1. Comparing printed text: $sformatf("%p", arr) on VCS ends with a space after the closing brace, so the text did not match a string typed by hand.
  2. A pattern inside ==: arr == '{0, 1, 2} is a compile error. Assign the pattern to an array first, then compare the two arrays.
  3. Printing a comparison: $sformatf("%0d", arr_a == arr_b) printed -1 for true on VCS, because the 1-bit result was treated as signed. Turn it into a word with ? "same" : "different".

Common mistakes to avoid

MistakeWhat you seeFix
Not marking a picked value as usedRepeats, with the right size (+BUG=1)Check and set the used table on every pick
A formula that is not one-to-oneRepeats, like i/2 giving pairsUse a formula where every i gives a different value
Pick until unused with a range barely bigger than the countSlow: many picks are rejected near the endUse fill and shuffle when you need most of the range
shuffle() on an associative arrayCompile errorUse a fixed array, dynamic array or queue
Checking only the sizeRepeats go unnoticedCount the different values

How AI helps you master this

  • Pick the method: describe how many values you need and from what range, and ask which of the four methods fits.
  • Check a formula: paste a fill formula and ask whether it can produce repeats.
  • Write the checker: ask for a function that counts different values in an array.

Warning: a convincing AI answer is not the same as a correct engineering result. Each of the three traps above looked fine on paper, so run the code.

Interview questions on unique values

  • How do you make an array of unique values without randomize()? Fill it with different values and call shuffle().
  • Can shuffle() create a repeat? No. It only changes the order of the values that are already there.
  • How do you pick 10 unique values from 0 to 9999? Pick with $urandom_range and skip values already in a used associative array.
  • How do you check an array has no repeats? Put each value into an associative array and compare its num() with the array size.
  • When would you still use a unique constraint? Inside a class, when the values must also meet other constraints.

More practice: SystemVerilog interview questions.

Practice challenge

  1. Run the simple example twice with different seeds and compare the shuffled order.
  2. Run the full bench, then +BUG=1, and list which values repeat.
  3. Change check 5 to i * 3 % 10. Is it one-to-one? Predict, then run.
  4. Bonus: make check 6 pick 10 values from 0 to 9999 and count how many picks were rejected.

FAQ

How do I make a unique array without random and constraints? Fill it with values that are all different, then call shuffle() to put them in a random order.

What does the shuffle() method do? It puts the elements of an unpacked array in a random order. The values themselves do not change.

Is fill and shuffle fully random? The order is random, but the set of values is fixed by your fill. For random values from a wide range, use pick until unused or a unique constraint.

When should I use shuffle instead of a unique constraint? When you only need a random order of a known set, especially outside a class.

What if my fill formula produces duplicates? Shuffle cannot fix that. The formula must give a different value for every index, which i/2 does not.

Does shuffle sort the array? No. shuffle() randomizes the order, and sort() puts it in ascending order.

References

  • IEEE 1800-2017, SystemVerilog standard: section 7.12.2 (array ordering methods, including shuffle() and sort()), section 18.13 ($urandom_range) and section 18.5.5 (uniqueness constraints).

Keep learning