Get randc Behavior from a rand Variable (Tested on VCS)
In short: randc hands out every value once before any value repeats. You can get the same from a rand variable in three ways: keep a list of values already used and forbid them, keep a pool of values not used yet and pick a position in it, or shuffle all the values and pop them one by one. On VCS, all three gave clean rounds, but over 1024 values the history list took 680 ms, the pool 112 ms and plain randc 14 ms.
Interviewers love this one, and it’s on our list of SystemVerilog interview questions. It looks like a trick question about randc, but it’s really a question about how the solver works.
To answer it properly I used one small example all the way through: a 2-bit x, so the values are 00, 01, 10 and 11. Every result below comes from one UVM test run on VCS X-2025.06-SP1.
Four tickets in a bag
Write 00, 01, 10 and 11 on four tickets and drop them in a bag. A round is four draws.
randc is the rule “keep each ticket out until the bag is empty, then put all four back”. You never see the same ticket twice in one round.
There are three ways to copy that rule by hand:
- The history method: put each ticket back after you draw it, but write it on a list. Before each draw, check the list and only accept a ticket that isn’t on it.
- The pool method: really take the ticket out. The bag only ever holds what’s left, so there’s no list to check.
- Shuffle and pop: tip all four tickets out, shuffle them into a line, and take them from the front.
What randc does
Here’s the built-in version first, so we know what we’re copying:
class tav_randc_data;
randc bit [1:0] x;
endclass
20 calls to randomize(), split into rounds of 4:
[TAV_1] randc, 20 calls: 11 00 10 01 | 01 00 11 10 | 10 11 00 01 | 01 00 10 11 | 10 00 01 11
[TAV_1] every group of 4 is 00..11 once: 1
Every round holds each value once, in a new order. That’s the behaviour the next three methods need to match.
Method 1: the history method
Keep a queue q of the values used this round. A constraint forbids x from being any of them, and post_randomize() adds the new value. When the queue holds all 4, clear it and a new round starts.
class tav_hist_data;
rand bit [1:0] x;
bit [1:0] q[$]; // values picked so far
int max_elements = 4;
constraint c { !(x inside {q}); }
function void post_randomize();
q.push_back(x);
if (q.size() == max_elements) q.delete(); // round done, start over
endfunction
endclass

[TAV_2] history + not inside, 20 calls: 11 01 00 10 | 11 01 00 10 | 00 01 11 10 | 10 00 11 01 | 11 10 01 00
[TAV_2] every group of 4 is 00..11 once: 1, every randomize() = 1: 1
It works. Notice the first two rounds came out in the same order. That’s allowed: like randc, it only promises no repeats inside a round, not a new order every round.
One catch hides in the constraint. Every call, the solver has to check x against everything in q. With 4 values that’s nothing, but the list grows with the range.
Method 2: the pool method
Turn it around. Keep a queue of the values not picked yet, and let the solver pick only a position in it, rand_index. x isn’t rand at all.
class tav_randomize_data;
bit [1:0] x;
rand int rand_index;
bit [1:0] q[$]; // values not picked yet
constraint c {
rand_index >= 0;
rand_index < q.size();
}
function void pre_randomize();
if (q.size() == 0) // new round: refill with all values
for (int count = 0; count < 4; count++) q.push_back(count);
endfunction
function void post_randomize();
x = q[rand_index]; // the picked value
q[rand_index] = q[q.size() - 1]; // move the last value into its slot
q.delete(q.size() - 1); // and drop the last slot
endfunction
endclass
The last two lines are the neat part. Deleting from the middle of a queue leaves a gap to close up, so instead the last value moves into the gap and the end is dropped. The order inside the pool doesn’t matter, because the next pick is random anyway.
Here’s the first round, step by step, as VCS printed it:
[TAV_3] first cycle, step by step: [idx 1 -> x 01, left '{'h0, 'h3, 'h2} ] [idx 1 -> x 11, left '{'h0, 'h2} ] [idx 0 -> x 00, left '{'h2} ] [idx 0 -> x 10, left '{}]

Call 1 picked position 1 (01), and 11 moved from the end into that slot. Call 2 picked position 1 again, which now held 11. After four calls the pool is empty, and the next pre_randomize() refills it.
[TAV_3] pool + rand_index, 20 calls: 01 11 00 10 | 01 11 00 10 | 10 00 11 01 | 00 10 01 11 | 01 00 10 11
[TAV_3] every group of 4 is 00..11 once: 1, every randomize() = 1: 1
Every round is clean. The solver only ever deals with one small range, never a list.
Method 3: shuffle and pop
If you don’t need the solver at all, you don’t have to use it. Fill a queue, call shuffle(), and pop from the front:
if (pool.size() == 0) begin
for (int i = 0; i < 4; i++) pool.push_back(i);
pool.shuffle();
end
x = pool.pop_front();
[TAV_7] shuffle + pop, 20 calls: 11 10 01 00 | 11 00 10 01 | 01 10 11 00 | 01 10 00 11 | 11 01 10 00
[TAV_7] every group of 4 is 00..11 once: 1
The downside: it lives outside randomize(), so it can’t be mixed with other constraints in the same call. If you need to steer which values come up more often, that’s a job for dist instead.
Which one is fastest?
With 4 values, all of them are instant. So I ran one full round of 1024 values (a 10-bit x) through each, and timed it with the wall clock:
[TAV_8] 1024 calls, history + not inside: distinct = 1024, 680 ms
[TAV_8] 1024 calls, pool + rand_index: distinct = 1024, 112 ms
[TAV_8] 1024 calls, randc bit [9:0]: distinct = 1024, 14 ms

All three gave 1024 different values. The history method was about 6 times slower than the pool, because each call checks against a longer list. Plain randc beat both by a wide margin.
These are wall-clock times from one run on a shared EDA Playground server, so treat them as a rough ratio, not a benchmark.
Which method to use
randc | History | Pool | Shuffle and pop | |
|---|---|---|---|---|
| No repeats in a round | yes | yes | yes | yes |
| Extra code | none | a queue + post_randomize | a queue + pre and post_randomize | a few lines |
| 1024 values, this run | 14 ms | 680 ms | 112 ms | no solver |
| You control when a round ends | no | yes | yes | yes |
Works with other constraints in one randomize() | yes | yes | yes | no |
My rule of thumb: use randc if it fits. If the variable has to stay rand, the pool method is the better hand-made answer. Shuffle is fine for a plain list of values.
Common mistakes
Setting the limit wrong
The history method only works if the limit is the real number of values. I set it to 3 instead of 4:
[TAV_6] max_elements = 3, 20 calls: 11 10 00 01 | 11 00 00 10 | 01 10 01 11 | 01 11 10 00 | 10 11 11 10
[TAV_6] every group of 4 is 00..11 once: 0
The list cleared one draw early, so values came back too soon. 00 even came out twice in a row. Work the limit out from the type instead: 2 ** $bits(x).
Clearing only when randomize() fails
Some answers skip the limit and wait for randomize() to fail once every value is used. It works, but look at the cost:
[TAV_5] 4 calls: 01 00 11 10 , q.size() = 4
Error-[CNST-CIF] Constraints inconsistency failure
[TAV_5] 5th randomize() = 0, x still = 10, pre_randomize ran 5 times, post_randomize ran 4 times
[TAV_5] after q.delete(): randomize() = 1, x = 10
Every round ends with a solver error in the log, which hides real failures. x keeps its old value, and post_randomize() doesn’t run on the failed call. Use a limit, and keep solver errors for real problems like the ones in why SystemVerilog randomization fails.
Leaving out rand_index >= 0
rand int is signed, so nothing in the language stops the solver picking a negative rand_index. I removed rand_index >= 0 to see what VCS would do:
[TAV_4] negative rand_index picked 0 times, every group of 4 clean: 1
In this run, VCS never picked a negative index, so nothing broke. Don’t count on that: keep the constraint, or declare rand int unsigned rand_index.
Calling it a “unique” constraint
!(x inside {q}) is an inside constraint, negated. unique is a different keyword that makes the elements of an array differ from each other, covered in array of unique random values.
The bench
One file, tav_randc.sv, runs all 8 parts as one UVM test. Every result prints with `uvm_info. On EDA Playground, paste it into testbench.sv, leave design.sv empty, pick UVM 1.2 and press Run (download it as a zip).
// tav_randc.sv
// The Art of Verification: get randc behaviour from a rand variable, measured
// A 2-bit x (values 00, 01, 10, 11), randomized 20 times, two ways:
// the history method (queue of values used + "not inside" constraint), and
// the pool method (queue of values not picked yet + a rand index).
// One UVM test, 8 parts. Every TAV result is printed with `uvm_info.
// EDA Playground: paste into testbench.sv, leave design.sv empty,
// pick UVM 1.2 and press Run. Part 5 makes randomize() fail on purpose,
// so one solver error there is expected. Part 4 may print out-of-bounds
// queue warnings; that is the point of part 4.
`include "uvm_macros.svh"
import uvm_pkg::*;
// ---------------------------------------------------------------
// Part 1: the built-in randc, for comparison
// ---------------------------------------------------------------
class tav_randc_data;
randc bit [1:0] x;
endclass
// ---------------------------------------------------------------
// Part 2: the history method. Remember every value picked this cycle
// in q, and forbid the next x from being one of them.
// ---------------------------------------------------------------
class tav_hist_data;
rand bit [1:0] x;
bit [1:0] q[$]; // values picked so far
int max_elements = 4; // part 6 sets a wrong value
constraint c { !(x inside {q}); }
function void post_randomize();
q.push_back(x);
if (q.size() == max_elements) q.delete(); // cycle done, start over
endfunction
endclass
// ---------------------------------------------------------------
// Part 3: the pool method. q holds the values NOT picked yet.
// The solver only picks rand_index; x is not rand at all.
// ---------------------------------------------------------------
class tav_randomize_data;
bit [1:0] x;
rand int rand_index;
bit [1:0] q[$]; // values not picked yet
constraint c {
rand_index >= 0;
rand_index < q.size();
}
function void pre_randomize();
if (q.size() == 0) // new cycle: refill with all values
for (int count = 0; count < 4; count++) q.push_back(count);
endfunction
function void post_randomize();
x = q[rand_index]; // the picked value
q[rand_index] = q[q.size() - 1]; // move the last value into its slot
q.delete(q.size() - 1); // and drop the last slot
endfunction
endclass
// ---------------------------------------------------------------
// Part 4: the same, but with rand_index >= 0 left out
// ---------------------------------------------------------------
class tav_randomize_data_no_lower;
bit [1:0] x;
rand int rand_index;
bit [1:0] q[$];
constraint c { rand_index < q.size(); } // a signed int can go negative
function void pre_randomize();
if (q.size() == 0)
for (int count = 0; count < 4; count++) q.push_back(count);
endfunction
function void post_randomize();
x = q[rand_index];
q[rand_index] = q[q.size() - 1];
q.delete(q.size() - 1);
endfunction
endclass
// ---------------------------------------------------------------
// Part 5: the history method with no limit; the caller clears on failure
// ---------------------------------------------------------------
class tav_hist_nolimit;
rand bit [1:0] x;
bit [1:0] q[$];
int n_pre, n_post;
constraint c { !(x inside {q}); }
function void pre_randomize(); n_pre++; endfunction
function void post_randomize(); n_post++; q.push_back(x); endfunction
endclass
// ---------------------------------------------------------------
// Part 8: the three methods on a 10-bit value (1024 values), for timing
// ---------------------------------------------------------------
class tav_hist10;
rand bit [9:0] x;
bit [9:0] q[$];
constraint c { !(x inside {q}); }
function void post_randomize();
q.push_back(x);
if (q.size() == 1024) q.delete();
endfunction
endclass
class tav_pool10;
bit [9:0] x;
rand int rand_index;
bit [9:0] q[$];
constraint c { rand_index >= 0; rand_index < q.size(); }
function void pre_randomize();
if (q.size() == 0) for (int i = 0; i < 1024; i++) q.push_back(i);
endfunction
function void post_randomize();
x = q[rand_index];
q[rand_index] = q[q.size() - 1];
q.delete(q.size() - 1);
endfunction
endclass
class tav_randc10;
randc bit [9:0] x;
endclass
// ---------------------------------------------------------------
// The test
// ---------------------------------------------------------------
class tav_randc_test extends uvm_test;
`uvm_component_utils(tav_randc_test)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// 1 if q[from .. from+n-1] holds each of 0 .. n-1 exactly once
function automatic bit is_perm(int q[$], int from, int n);
bit seen[int];
for (int i = from; i < from + n; i++) begin
if (q[i] < 0 || q[i] >= n || seen.exists(q[i])) return 0;
seen[q[i]] = 1;
end
return 1;
endfunction
// the values as 2-bit binary, in groups of 4
function automatic string bin_q(int q[$]);
string s = "";
foreach (q[i]) begin
s = {s, $sformatf("%b ", 2'(q[i]))};
if (i % 4 == 3 && i != q.size() - 1) s = {s, "| "};
end
return s;
endfunction
function automatic int n_distinct(int q[$]);
bit seen[int];
foreach (q[i]) seen[q[i]] = 1;
return seen.num();
endfunction
// 1 if every full group of 4 is a clean cycle
function automatic bit all_cycles_ok(int q[$]);
for (int i = 0; i + 4 <= q.size(); i += 4) if (!is_perm(q, i, 4)) return 0;
return 1;
endfunction
virtual task run_phase(uvm_phase phase);
phase.raise_objection(this);
part1(); part2(); part3(); part4();
part5(); part6(); part7(); part8();
phase.drop_objection(this);
endtask
// Part 1: randc, 20 calls
function void part1();
tav_randc_data d = new();
int q[$];
repeat (20) begin void'(d.randomize()); q.push_back(d.x); end
`uvm_info("TAV_1", $sformatf("randc, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_1", $sformatf("every group of 4 is 00..11 once: %0d", all_cycles_ok(q)), UVM_LOW)
endfunction
// Part 2: the history method, 20 calls
function void part2();
tav_hist_data d = new();
int q[$];
bit ok, all_ok = 1;
repeat (20) begin ok = d.randomize(); all_ok &= ok; q.push_back(d.x); end
`uvm_info("TAV_2", $sformatf("history + not inside, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_2", $sformatf("every group of 4 is 00..11 once: %0d, every randomize() = 1: %0d", all_cycles_ok(q), all_ok), UVM_LOW)
endfunction
// Part 3: the pool method, 20 calls, with the pool shown each time
function void part3();
tav_randomize_data d = new();
int q[$];
bit ok, all_ok = 1;
string trace = "";
repeat (20) begin
ok = d.randomize();
all_ok &= ok;
q.push_back(d.x);
if (q.size() <= 4)
trace = {trace, $sformatf("[idx %0d -> x %b, left %p] ", d.rand_index, d.x, d.q)};
end
`uvm_info("TAV_3", $sformatf("pool + rand_index, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_3", $sformatf("every group of 4 is 00..11 once: %0d, every randomize() = 1: %0d", all_cycles_ok(q), all_ok), UVM_LOW)
`uvm_info("TAV_3", $sformatf("first cycle, step by step: %s", trace), UVM_LOW)
endfunction
// Part 4: rand_index >= 0 left out
function void part4();
tav_randomize_data_no_lower d = new();
int q[$];
int n_neg = 0;
repeat (20) begin
void'(d.randomize());
if (d.rand_index < 0) n_neg++;
q.push_back(d.x);
end
`uvm_info("TAV_4", $sformatf("no rand_index >= 0, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_4", $sformatf("negative rand_index picked %0d times, every group of 4 clean: %0d", n_neg, all_cycles_ok(q)), UVM_LOW)
endfunction
// Part 5: no limit, clear the history when randomize() fails
function void part5();
tav_hist_nolimit d = new();
int q[$];
bit ok;
repeat (4) begin void'(d.randomize()); q.push_back(d.x); end
`uvm_info("TAV_5", $sformatf("4 calls: %s, q.size() = %0d", bin_q(q), d.q.size()), UVM_LOW)
`uvm_info("TAV_5", "5th call, no legal value left (a solver error is expected next):", UVM_LOW)
ok = d.randomize();
`uvm_info("TAV_5", $sformatf("5th randomize() = %0d, x still = %b, pre_randomize ran %0d times, post_randomize ran %0d times",
ok, d.x, d.n_pre, d.n_post), UVM_LOW)
d.q.delete();
ok = d.randomize();
`uvm_info("TAV_5", $sformatf("after q.delete(): randomize() = %0d, x = %b", ok, d.x), UVM_LOW)
endfunction
// Part 6: the history method with the wrong limit (3 instead of 4)
function void part6();
tav_hist_data d = new();
int q[$];
d.max_elements = 3;
repeat (20) begin void'(d.randomize()); q.push_back(d.x); end
`uvm_info("TAV_6", $sformatf("max_elements = 3, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_6", $sformatf("every group of 4 is 00..11 once: %0d", all_cycles_ok(q)), UVM_LOW)
endfunction
// Part 7: no solver at all: shuffle the four values, then pop
function void part7();
int pool[$], q[$];
repeat (20) begin
if (pool.size() == 0) begin
for (int i = 0; i < 4; i++) pool.push_back(i);
pool.shuffle();
end
q.push_back(pool.pop_front());
end
`uvm_info("TAV_7", $sformatf("shuffle + pop, 20 calls: %s", bin_q(q)), UVM_LOW)
`uvm_info("TAV_7", $sformatf("every group of 4 is 00..11 once: %0d", all_cycles_ok(q)), UVM_LOW)
endfunction
// wall clock in ms, from the shell; -1 if $system is not allowed
function automatic longint now_ms();
int fd, rc;
longint ns = -1;
rc = $system("date +%s%N > tav_now.txt");
fd = $fopen("tav_now.txt", "r");
if (fd == 0) return -1;
void'($fscanf(fd, "%d", ns));
$fclose(fd);
return (ns < 0) ? -1 : ns / 1000000;
endfunction
// Part 8: does the history method get slower as the list grows?
// 1024 values, one full cycle each, timed with the wall clock
function void part8();
tav_hist10 h = new();
tav_pool10 p = new();
tav_randc10 c = new();
int qh[$], qp[$], qc[$];
longint t0, t1, t2, t3;
t0 = now_ms();
repeat (1024) begin void'(h.randomize()); qh.push_back(h.x); end
t1 = now_ms();
repeat (1024) begin void'(p.randomize()); qp.push_back(p.x); end
t2 = now_ms();
repeat (1024) begin void'(c.randomize()); qc.push_back(c.x); end
t3 = now_ms();
`uvm_info("TAV_8", $sformatf("1024 calls, history + not inside: distinct = %0d, %0d ms", n_distinct(qh), t1 - t0), UVM_LOW)
`uvm_info("TAV_8", $sformatf("1024 calls, pool + rand_index: distinct = %0d, %0d ms", n_distinct(qp), t2 - t1), UVM_LOW)
`uvm_info("TAV_8", $sformatf("1024 calls, randc bit [9:0]: distinct = %0d, %0d ms", n_distinct(qc), t3 - t2), UVM_LOW)
endfunction
endclass
module tav_tb;
initial run_test("tav_randc_test");
endmodule
What VCS printed
VCS X-2025.06-SP1 with UVM 1.2. The run ended with UVM_ERROR : 0 and UVM_WARNING : 0. The only error was the intended Error-[CNST-CIF] in part 5. Every result is shown above with the UVM_INFO prefix trimmed.
Where AI helps, and where it doesn’t
Ask an AI assistant for “randc from rand” and you’ll usually get the history method, often with the constraint called “unique”. It rarely mentions that the list grows, or that clearing on failure fills your log with solver errors.
Use it to draft the class, then check two things yourself: that the limit matches the real number of values, and how long a full round takes at your real width.
Practice challenge
Change the pool method to work for any width, using a parameter W and 2 ** W values. Then time one full round at W = 12 against the history method. Does the gap grow?
FAQ
How do I get randc behavior from a rand variable?
Keep a pool of the values not used yet, randomize an index into it, take that value and remove it from the pool. Refill the pool when it’s empty. A history list of used values also works, but it gets slower as the range grows.
Why is the history method slow?
Every call carries a constraint against every value used so far. Over 1024 values on VCS, it took 680 ms against 112 ms for the pool method.
Why not just use randc?
You should when you can: it was the fastest in this run, at 14 ms for 1024 values. The hand-made methods are for when the variable has to stay rand, or you need to decide yourself when a round ends.
Does randc give a new order every round?
It gives no repeats inside a round. The order of the next round is random, so it can match the last one.
What happens when randomize() fails?
It returns 0, the variable keeps its old value, and post_randomize() doesn’t run. VCS also prints the failing constraints.
References
- IEEE 1800-2023, SystemVerilog standard: clause 18.4.2 (randc), clause 18.6.2 (pre_randomize and post_randomize), clause 18.5.5 (inside), clause 7.12.2 (array ordering methods, including shuffle).
Read next
- randomize() vs std::randomize()
- Array of unique random values
- Why SystemVerilog randomization fails
- SystemVerilog interview questions
- Constraint override in SystemVerilog
- Enable or disable a specific constraint
Part of the learning path SystemVerilog for verification

Another way I tried.
class rranc ;
rand bit [15:0] val;
int qval[$];
constraint c_r{
!(val inside {qval}) ;
}
function void post_randomize();
if(qval.size >= 2**$bits(val)-1)
qval = {};
qval.push_back(val);
endfunction
endclass
module top;
initial begin
rranc r = new ;
repeat(32) begin
r.randomize();
end
$display(“%p”,r);
end
endmodule