# Dynamic array randomization

**URL:** <https://verificationacademy.com/forums/t/dynamic-array-randomization/35915>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint, Dynamic-Array, array-sum\
**Created:** [March 29, 2019, 11:53pm UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915 "2019-03-29T23:53:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharatk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@sharatk](https://verificationacademy.com/forums/u/sharatk)\
**Post date:** [March 29, 2019, 11:53pm UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/1 "2019-03-29T23:53:08Z")

</div>

Please consider the class code below.

```verilog
class c;
   rand int arr[];
   constraint C1 { foreach(arr[i]) {
                        arr[i] < 5;
                        arr[i] > 0;
                    } } 
   constraint C2 { arr.sum() == 14; } 
   //constraint C3 { arr.size < 5; } 
endclass

```

When I try to randomize it, it fails. I am using IUS, but I tried it VCS from edaplayground.  
When I push in C3 constraint, VCS works with it, but IUS does not. Both works if C3 has equality(==) instead of \< . Can somebody please explain if LRM prohibits it and what is the work-around for such issues.  
Also, if someone has MTI, please see if it works for you. I am passing a small module, in case you don’t want to write your own.

```verilog
module test;
   c obj = new;
    initial begin
        obj.randomize();
        $display("%p ", obj.arr);
        $finish(2);
    end
endmodule

```

---

<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:** [March 30, 2019, 12:52am UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/2 "2019-03-30T00:52:59Z")

</div>

_In reply to [sharatk](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915):_

Your C2 constraint fails because there are no elements in the   
arr array to produce a sum equaling 14. You need an active constraint on the size of an array for a call to   
randomize() to change the size of a dynamic array.

The value for the size of an array gets chosen and becomes a state variable before constructing the array and evaluating constraints on any elements. If you run your simulations with different seeds, or add a repeat loop, you will see that only solutions choosing a size of 4 succeed. It’s a matter of chance which simulators choose a solution that passes on the first try.

So for this example, you probably want to add another constraint for the minimum array size.

```verilog
arr.size * 4 >=14

```

---

<div class="post-metadata">

**Author:** ![sharatk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@sharatk](https://verificationacademy.com/forums/u/sharatk)\
**Post date:** [March 30, 2019, 1:28am UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/3 "2019-03-30T01:28:39Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/2):_

Thanks Dave,  
“You will see that only solutions choosing a size of 4 succeed.”  
Even a size of 14, will succeed, isnt it?

I added  
constraint C3 { arr.size \>= 4; arr.size \<= 14; }  
and now it seems to work.  
Thanks for all your help.

How do we know this? Is it an arcane interpretation of LRM?  
“The value for the size of an array gets chosen and becomes a state variable”

---

<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:** [March 30, 2019, 2:29am UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/4 "2019-03-30T02:29:36Z")

</div>

_In reply to [sharatk](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/3):_

> The size of a dynamic array or queue declared as rand or randc can also be constrained. In that case, the array shall be resized according to the size constraint, _and then_ all the array elements shall be randomized

> If an array is constrained by both size constraints and iterative constraints, the size constraints are solved first and the iterative constraints next. As a result of this implicit ordering between size constraints and iterative constraints, _the size method shall be treated as a state variable_ within the foreach block of the corresponding array.

---

<div class="post-metadata">

**Author:** ![sharatk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@sharatk](https://verificationacademy.com/forums/u/sharatk)\
**Post date:** [March 30, 2019, 2:49am UTC](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/5 "2019-03-30T02:49:25Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/dynamic-array-randomization/35915/4):_

Thanks Dave.
