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 Class Properties

SystemVerilog Randomization

Runnable example from the SystemVerilog Randomization tutorial (Randomizing Class Properties)

Open in IDE
testbench.sv
class myPacket;
  rand bit [3:0] mode;   // no constraint - any value 0 to 15 is equally likely
endclass

module tb_top;
  myPacket pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    $display("mode = %0d", pkt.mode);
  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.