SystemVerilog Queue Randomization

Randomize Queue SystemVerilog

In most of the queue use cases, queue is used as buffer or temporary storage. so there wont be much need to randomize queue.

randomize queue size

In below example, queue size will get randomized based on size constraint, and queue elements will get random values

  1. Declare queue with rand
  2. On randomization queue will get random values
class queue_rand;
  rand bit [7:0] qu[$];
  
  constraint size_c  { qu.size() inside {[4:10]}; }
  
  function void display();
    $display("qu size is = %0d",qu.size());
    $display("qu = %p",qu);
  endfunction
endclass

program queue_randomization;
  queue_rand pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

Simulator Output

qu size is = 7
qu = '{'h88, 'h9b, 'h9a, 'h10, 'h5f, 'hde, 'h84}

Click to execute on