# Randomizing a queue

**URL:** <https://verificationacademy.com/forums/t/randomizing-a-queue/30569>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, OVM, Randomize, OVM-Systemverilog\
**Created:** [April 21, 2015, 8:19pm UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569 "2015-04-21T20:19:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ub\_ovm](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@ub\_ovm](https://verificationacademy.com/forums/u/ub_ovm)\
**Post date:** [April 21, 2015, 8:19pm UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/1 "2015-04-21T20:19:10Z")

</div>

Hi,  
I am trying to randomly pick a string from a queue of strings and then pass that string on to a function call. Can I do that using randomize()? I am new to OVM , I am doing something similar to below , please let me know if that is correct:  
e.g  
dummy\_class arg;  
string dummy\_names= {“abc”, “efg”,“xyz”, “mno”};  
arg= new(std::randomize(dummy\_names), 1);  
obj.func(arg);  
So the randomize function should return me the index of the randomly chosen queue member if I can use it as above?  
Also can someone point out where to find documentation on this?  
Thanks in advance

---

<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:** [April 22, 2015, 6:01am UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/2 "2015-04-22T06:01:23Z")

</div>

A couple of problems with your code

```verilog
 string dummy_names= {"abc", "efg","xyz", "mno"};

```

Is legal, but dummy\_names is not a queue; it is just a simple string and you have concatinated 4 string literals into a single string It is the same as

```verilog
 string dummy_names= "abcefgxyzmno"};

```

Your other problems are that the randomize function only randomizes integral variables, not string variables, and that randomize returns non-zero on a successful randomization, and zero on failed randomization. It never returns the value of the randomized variable.

You have two options after you fix the queue declaration. You can either shuffle the queue and pick the first or last element of the queue, or you can pick a random index value

```verilog
string dummy_names[$]= {"abc", "efg","xyz", "mno"};
...
arg = new(dummy_args[$urandom_range(0,dummy_args.size()-1), 1);
//or
dummy_names.shuffle();
arg= new(dummy_names[0], 1);

```

---

<div class="post-metadata">

**Author:** ![Sushrut\_Veerapur](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/sushrut_veerapur/32/2127_2.png) [@Sushrut\_Veerapur](https://verificationacademy.com/forums/u/Sushrut_Veerapur)\
**Post date:** [April 22, 2015, 7:28am UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/3 "2015-04-22T07:28:48Z")

</div>

q.shuffle() is the simplest way to achieve this.

---

<div class="post-metadata">

**Author:** ![ub\_ovm](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@ub\_ovm](https://verificationacademy.com/forums/u/ub_ovm)\
**Post date:** [April 22, 2015, 1:36pm UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/4 "2015-04-22T13:36:14Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/3):_

Thanks a lot Dave. That queue thing was a typo. will review my code better from next time onwards before posting.

---

<div class="post-metadata">

**Author:** ![ub\_ovm](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/c57346/32.png) [@ub\_ovm](https://verificationacademy.com/forums/u/ub_ovm)\
**Post date:** [April 22, 2015, 1:36pm UTC](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/5 "2015-04-22T13:36:33Z")

</div>

_In reply to [Sushrut Veerapur](https://verificationacademy.com/forums/t/randomizing-a-queue/30569/4):_

Thanks a lot Sushrut
