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

Queue Randomization

SystemVerilog Randomization

A rand queue inside a class constrained to size() == 4 and then randomized.

Open in IDE
testbench.sv
class Packet;
  rand bit [3:0] 	queue [$]; 	// Declare a queue with "rand"
  
  // Constrain size of queue between 5 and 10  
  constraint c_array { queue.size() == 4; }
endclass

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



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

ncsim> run
queue = '{'hf, 'hf, 'h2, 'h9}
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.