Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Randomizing Plain Variables with std::randomize()

SystemVerilog Randomization

Runnable example from the SystemVerilog Randomization tutorial (Randomizing Plain Variables with std::randomize())

Open in IDE
testbench.sv
module tb_top;
  initial begin
    int addr, data;

    // Randomize plain (non-rand) local variables directly, with an inline constraint
    if (std::randomize(addr, data) with { addr inside {[0:255]}; data < 100; }) begin
      $display("addr = %0d data = %0d", addr, data);
    end else begin
      $display("randomization failed");
    end
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.