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

Static Arrays

SystemVerilog Randomization

A rand fixed-size array inside a class, randomized and printed with %p.

Open in IDE
testbench.sv
class Packet;
  rand bit [3:0] 	s_array [7]; 	// Declare a static array with "rand"
endclass

module tb;
  Packet pkt;
  
  // Create a new packet, randomize it and display contents
  initial begin
    pkt = new();
    pkt.randomize();
    $display("queue = %p", pkt.s_array);
  end 
endmodule



/*
Simulation Log:
---------------

ncsim> run
queue = '{'hf, 'hf, 'h2, 'h9, 'he, 'h4, 'ha}
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
*/
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.