# Randomization of dynamic array using "with"

**URL:** <https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063>\
**Category:** UVM\
**Tags:** UVM, SystemVerilog, Dynamic-Array, randomization, constraints\
**Created:** [April 21, 2017, 7:07pm UTC](https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063 "2017-04-21T19:07:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evilpascal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@evilpascal](https://verificationacademy.com/forums/u/evilpascal)\
**Post date:** [April 21, 2017, 7:07pm UTC](https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063/1 "2017-04-21T19:07:42Z")

</div>

Hi,

When I try randomizing a sequence like this:

```verilog
if(!seq.randomize() with {
  _num inside {[0:10]};
  _arr == '{8'h11, 8'h22, 8'h33};
}) begin
  `uvm_error("TST", "Randomization failed")
end

```

I get this error in Questa 10.5:

> (vopt-2936) Illegal non-integral expression in random constraint.

The variables are defined as follows:

```verilog
rand int _num;
rand logic [7:0] _arr [];

```

I have made an example of the problem on EDA Playground:

> **[EDA Playground](https://www.edaplayground.com/x/3sBb)**
>
> Edit, save, simulate, synthesize SystemVerilog, Verilog, VHDL and other HDLs from your web browser.

If you go there and comment:

```verilog
_arr == '{8'h11, 8'h22, 8'h33};

```

in testbench.sv and

```verilog
arr == _arr;

```

in a\_seq.svh, the code runs properly.

What is the problem with the code and is there correct way to achieve this, or an acceptable workaround?

---

<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, 2017, 7:57am UTC](https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063/2 "2017-04-22T07:57:26Z")

</div>

_In reply to [evilpascal](https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063):_  
The problem is as the error message tells you - constraint expressions need to be with integral expressions. An unpacked array is not an integral expression. you need to create a separate array, and then use a   
foreachloop and break the aggregate constraint into an set of integral constraints.

```verilog
bit [7:0] set_arr;

set_arr = '{8'h11, 8'h22, 8'h33};
if(!seq.randomize() with {
  _num inside {[0:10]};
  foreach (set_arr[ii]) _arr[ii] == set_arr[ii];
}) begin
  `uvm_error("TST", "Randomization failed")
end

```

---

<div class="post-metadata">

**Author:** ![evilpascal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@evilpascal](https://verificationacademy.com/forums/u/evilpascal)\
**Post date:** [April 22, 2017, 11:19am UTC](https://verificationacademy.com/forums/t/randomization-of-dynamic-array-using-with/33063/3 "2017-04-22T11:19:22Z")

</div>

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

Thanks for the soulution dave\_59. I just needed to also constrain the “\_arr” variable size to the size of the “set\_arr”, or there would be a failure when simulation is started:

```verilog
if(!seq.randomize() with {
  _num inside {[0:10]};
  _arr.size() == set_arr.size();
  foreach (set_arr[ii]) _arr[ii] == set_arr[ii];
}) begin
  `uvm_error("TST", "Randomization failed")
end

```
