title: Randomize a queue of objects in systemverilog
body: <p>I have seen techniques used to randomize queues of integral values, but I am having difficulties with queues of objects. An example code:</p>
  
  <pre><code>class int_queue_class;
    rand int x[$];
  
    function void print;
      $display($sformatf("Size: %0d", x.size()));
      foreach(x[i])
        $display($sformatf("x[%0d] = %0d", i, x[i]));
    endfunction : print
  
  endclass : int_queue_class
  
  
  class obj;
    rand int value;
  endclass : obj
  
  class obj_queue_class;
    rand obj x[$];
  
    function void print;
      $display($sformatf("Size: %0d", x.size()));
      foreach(x[i])
        $display($sformatf("x[%0d] = %0d", i, x[i].value));
    endfunction : print
  
  endclass : obj_queue_class
  
  
  program top();
    initial
    begin
      int_queue_class iqc;
      obj_queue_class oqc;
      iqc = new();
      iqc.randomize() with {x.size() == 5; x[2] == 5;};
      iqc.print();
      oqc = new();
      oqc.randomize() with {x.size() == 5; x[2].value == 5;};
      oqc.print();
    end
  endprogram : top
  </code></pre>
  
  <p>It seems that the same approach cannot be used when it comes to objects. Is there a way to randomize such a queue while keeping the ability to simultaneously constrain the queue size and individual element fields? The ideas that I've come up with that use pre/post randomize methods all loose at least one of those two options.</p>