# I have to randomize an queue without using constraints

**URL:** <https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint-randomization, FIFO-UVM-ConstrainedRandom, dynamicarray-constraint\
**Created:** [July 18, 2023, 5:25am UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952 "2023-07-18T05:25:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marathuteja](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/dec6dc/32.png) [@marathuteja](https://verificationacademy.com/forums/u/marathuteja)\
**Post date:** [July 18, 2023, 5:25am UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/1 "2023-07-18T05:25:49Z")

</div>

Hi There,

Can any one give inputs to this problem. I have to randomize a queue without using constraints.  
The queue is bounded queue for example the bounded queue has 20 elements and the queue each elements must be inside 0 to 10.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 18, 2023, 7:12am UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/2 "2023-07-18T07:12:37Z")

</div>

_In reply to [marathuteja](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952):_

What have you tried??

---

<div class="post-metadata">

**Author:** ![marathuteja](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/dec6dc/32.png) [@marathuteja](https://verificationacademy.com/forums/u/marathuteja)\
**Post date:** [July 18, 2023, 5:43pm UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/3 "2023-07-18T17:43:40Z")

</div>

I just tried this code

```verilog

class con;
  rand int a [$:4];
  int da [];
  
  function void post_randomize();
    foreach(a[i])
      a[i] = $urandom_range(20,30)
  endfunction
  
endclass

module tb;
  con c;
  initial
    begin
      c = new();
      assert(c.randomize);
      $display("size of ele is %d", c.a.size);
      $display("Queue = %p", c.a);
    end
endmodule

```

---

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [July 19, 2023, 8:35am UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/4 "2023-07-19T08:35:30Z")

</div>

_In reply to [marathuteja](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/3):_

A few comments on your code :

1. Since you have declared property ‘a’ as rand and there are no existing constraints , you could have randomized elements with values b/w 0-10
2. The $urandom\_range(20,30) call in post\_randomize doesn’t match your intention .
3. You haven’t constrained the size to 20 elements .

---

<div class="post-metadata">

**Author:** ![Venkata\_Raghu\_R](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/e47774/32.png) [@Venkata\_Raghu\_R](https://verificationacademy.com/forums/u/Venkata_Raghu_R)\
**Post date:** [July 19, 2023, 10:19am UTC](https://verificationacademy.com/forums/t/i-have-to-randomize-an-queue-without-using-constraints/41952/5 "2023-07-19T10:19:53Z")

</div>

Just wrote this code, it might fulfill your requirement:

```verilog

class rand_class;
  int q[$:9];
endclass

module test;
  rand_class rc;
  int p;
  
  initial begin
    rc = new();    
    while(rc.q.size() < 10) begin
      std::randomize(p) with {p inside {[0:10]};};
      rc.q.push_back(p);      
    end
    $display("Size of Queue: %0d", rc.q.size());
    $display("Queue: %0p", rc.q);
  end
endmodule

```

Output:  
Size of Queue: 10  
Queue: '{6, 0, 5, 6, 4, 6, 7, 5, 9, 0}
