# Questions on Array Reducation in Constraint

**URL:** <https://verificationacademy.com/forums/t/questions-on-array-reducation-in-constraint/38316>\
**Category:** SystemVerilog\
**Tags:** random-constraint, array-reducation, SystemVerilog, constraints-systemverilog\
**Created:** [November 6, 2020, 7:04pm UTC](https://verificationacademy.com/forums/t/questions-on-array-reducation-in-constraint/38316 "2020-11-06T19:04:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![qkuang](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/q/f17d59/32.png) [@qkuang](https://verificationacademy.com/forums/u/qkuang)\
**Post date:** [November 6, 2020, 7:04pm UTC](https://verificationacademy.com/forums/t/questions-on-array-reducation-in-constraint/38316/1 "2020-11-06T19:04:25Z")

</div>

I am quite confused about the with clause, this is the question description and answer from other forum post:  
Q: Write a constraint so that 10 elements of the array arr have the value of 5.  
A:

```verilog

   class numberGen;
   int i,j;
   rand bit[7:0] arr[100];
   function new();
      i = 5; j =10;
   endfunction
   constraint pick_ij { arr.sum(item) with (int'(item == i)) == j;}
endclass : numberGen

```

My understanding of this code is: for every item in arr, if it equal to i, then sum up the comparison results and constrain to j, because the comparison results are 1-bit, so we need a int cast.

Then I got another confusion on other post,

> <https://stackoverflow.com/questions/59189701/array-slicing-in-inside-operator-in-system-verilog-constraints>

Q:Write constraint make a value have a value inside array with index ranging from 3 to count:  
A:Answered by Dave

```verilog

    class pkt;
        rand bit [3:0] array[12];
        rand bit [3:0] value;
        int count;
        function new(int cnt);
            count = cnt;
            this.randomize();
        endfunction: new

        constraint c {
          array.or() with (item.index inside {[3:count]} && item == value);// **hard to understand the condition after &&**
        }
    endclass: pkt

```

For this with clause, I understand the first part: for every item which index not less than 3, but I have difficulty to understand the second part, does it constrain the every element to value? or it is a if condition check like the first example? what if we swap the variable like value == item?

Looking forward to the detail reply.  
Thank you!!

---

<div class="post-metadata">

**Author:** ![cahuja](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@cahuja](https://verificationacademy.com/forums/u/cahuja)\
**Post date:** [December 2, 2020, 1:58am UTC](https://verificationacademy.com/forums/t/questions-on-array-reducation-in-constraint/38316/2 "2020-12-02T01:58:25Z")

</div>

_In reply to [kuangqq08](https://verificationacademy.com/forums/t/questions-on-array-reducation-in-constraint/38316):_

```auto
class numberGen;
   int i,j;
  rand bit[7:0] arr[25];
  rand bit num_10[15];
   function new();
      i = 5; j =10;
   endfunction
  constraint c1 {
    foreach(arr[i]) 
      if(arr[i]==10) num_10[i]==1;
    else num_10[i] == 0;
  }
  constraint c2 {
    num_10.sum() with (int'(item)) == 10;
  }
endclass : numberGen

```

This is working for me.
