Random in SystemVerilog: How It Works

How-to basics · Updated November 23, 2026

In SystemVerilog, random refers to the built-in randomization features that generate pseudo-random values for variables and objects. The core mechanism is the rand and randc keywords inside classes, combined with the randomize() method and the $random system function. This article explains how randomization works, how to control it, and common pitfalls.

Background: Why Randomization Matters

Randomization is central to constrained-random verification (CRV), the dominant methodology for verifying digital hardware. Instead of writing directed test vectors for every scenario, engineers write classes with random variables and constraints. The simulator then generates many random combinations, covering a wide input space automatically. This approach finds corner-case bugs that directed tests miss, especially in complex protocols like PCIe, Ethernet, and memory controllers.

SystemVerilog’s randomization differs from simple $random in C or Python’s random module. It is integrated with the language’s object-oriented features, allowing constraints to be declared alongside the data they affect. The solver uses a constraint solver to find values that satisfy all active constraints, not just a uniform distribution.

How Randomization Works: rand, randc, and randomize()

In a class, you declare random variables with rand (random) or randc (random cyclic). rand gives a new random value each time; randc cycles through all possible values before repeating any, which is useful for exhaustive coverage of small ranges.

class Packet;
  rand bit [7:0] length;
  randc bit [3:0] id;
  constraint c_length { length > 0; length < 256; }
endclass

Packet p = new();
if (p.randomize()) begin
  $display("length=%0d id=%0d", p.length, p.id);
end

The randomize() method returns 1 if successful, 0 if the constraints are unsatisfiable. It assigns random values to all rand and randc variables, respecting the constraints. You can also call randomize() with arguments to randomize only specific variables, e.g., p.randomize(length).

Under the hood, the simulator’s solver uses a pseudo-random number generator (PRNG) seeded from the simulator’s command-line option (like +ntb_random_seed in some tools) or the set_randstate method. The default seed is often derived from the system time, making runs non-reproducible unless you set a fixed seed.

Constraints: The Power of Randomization

Constraints limit the random values to a legal set. They are declared with constraint blocks inside the class. You can use relational operators (<, >, ==), logical operators (&&, ||), and set membership (inside).

class Address;
  rand bit [31:0] addr;
  constraint c_aligned { addr % 4 == 0; }
  constraint c_range { addr inside {[32'h0000_0000 : 32'h0000_FFFF]}; }
endclass

You can also use solve ... before ... to control the distribution order, and dist to specify weighted distributions. For example, addr dist {0 := 1, [1:10] :/ 5} gives a weight of 1 to 0 and a total weight of 5 spread over 1 to 10.

Constraints can be turned on and off with constraint_mode(). For instance, p.c_length.constraint_mode(0) disables that constraint for the next randomize(). This is handy for stress testing.

Choosing Between $random and Class Randomization

SystemVerilog provides several system functions for simple random numbers:

  • $random – returns a 32-bit signed integer. Use $random % 100 to get 0-99, but beware of modulo bias.
  • $urandom – returns a 32-bit unsigned integer, with a better distribution than $random.
  • $urandom_range(max, min) – returns an unsigned integer in [min, max], inclusive, with no bias.

For quick, unconstrained values in testbenches, $urandom_range is the cleanest. For complex, constrained random data, use class-based randomization. A common mistake is using $random for values that must meet protocol requirements (like valid opcodes) – that leads to invalid tests. Instead, define a class with constraints.

Common Problems and Pitfalls

One frequent issue is unsatisfiable constraints. If the solver cannot find a value, randomize() returns 0. Debug by checking the return value and using print or display to see the constraint state. Another pitfall is forgetting to declare a variable as rand – then it won’t be randomized. Also, constraints that are too tight (e.g., length > 10 && length < 5) cause failures.

Another issue is seed reproducibility. Without a fixed seed, you can’t reproduce a failing test. Use the simulator’s seed option (e.g., -sv_seed in some tools) and store it in the log. Some simulators allow $srandom(seed) to set the seed for a thread.

Finally, be careful with randc and large ranges – it cycles through all values, which can be slow for 32-bit variables. Use randc only for small enumerations.

Distributions and Weighted Randomization

Sometimes you want some values more often. Use the dist operator inside a constraint:

rand int opcode;
constraint c_op { opcode dist {0 := 1, 1 := 2, [2:3] :/ 5}; }

Here, opcode 0 has weight 1, opcode 1 has weight 2, and the range 2-3 has total weight 5, split evenly (2.5 each). This is useful for biasing towards certain operations in a processor testbench.

You can also use randcase for procedural random branching, but that’s not part of class randomization. For example:

randcase
  1: $display("rare");
  9: $display("common");
endcase

Randomization in Interfaces and Modules

Randomization is not limited to classes. You can randomize variables in a module or interface using std::randomize with a scope:

module test;
  int data;
  initial begin
    if (!std::randomize(data) with { data > 0; data < 100; })
      $error("Randomization failed");
  end
endmodule

This is handy for quick randomization without defining a class. The with clause adds inline constraints. Note that std::randomize returns 1 on success, 0 on failure.

Frequently Asked Questions

How do I set a fixed seed in SystemVerilog simulation? Use the simulator’s command-line option, typically -sv_seed (for Synopsys VCS) or -seed (for Cadence Xcelium). For example, vcs -sv_seed 12345. Inside the code, you can call $srandom(seed) on a process to set its local seed.

What is the difference between rand and randc? rand gives a new random value each time, with no memory of previous values. randc cycles through all possible values in a random order, ensuring each value appears once before any repeats. Use randc for small enumerations to guarantee coverage.

Can I randomize a variable that is not declared as rand? Yes, you can pass it to randomize() as an argument, e.g., obj.randomize(variable), or use std::randomize(variable). However, it must be a variable in the class or in scope, and it will be randomized even without the rand keyword.

Why does randomize() return 0? It returns 0 when the constraint solver cannot find any value satisfying all active constraints. This can happen if constraints conflict (e.g., a > 5 and a < 3) or if the variable’s type range is too small. Check the constraints and the variable ranges.

Is $random the same as rand in a class? No. $random is a system function that returns a pseudo-random integer, with no constraints. Class rand variables are randomized by the randomize() method, which respects constraints and can be controlled per-object. For constrained randomization, always use classes.

How do I randomize an array? Declare the array as rand and use a constraint to set its size or values. For example, rand bit [7:0] data[]; and constraint c_size { data.size() inside {[1:10]}; }. Then randomize() fills the array with random values. You can also use foreach in constraints to relate elements.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.