# Constraint number of occurrences in dynamic array

**URL:** <https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087>\
**Category:** SystemVerilog\
**Tags:** systemverilog-arrays-struct-constraint-randomization-indexes, SystemVerilog, systemverilog-constraint, Assertion-system-verilog, mdarray-constraint-foreachconstraint-arrayliteral\
**Created:** [May 17, 2021, 9:29pm UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087 "2021-05-17T21:29:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blitzz0418](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/8c91f0/32.png) [@Blitzz0418](https://verificationacademy.com/forums/u/Blitzz0418)\
**Post date:** [May 17, 2021, 9:29pm UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/1 "2021-05-17T21:29:47Z")

</div>

Hi ,

i am trying to restrict the number of occurence of few values in my array?  
somehow the constraint with arr.sum() with (item==3) = 3\*4 ; does seems to be working. - sim hangs.  
what i want to do is  
restrict element 3 in array for say 4 times and not more than that.  
Can someone help?  
i also tried storing it in queue and restricting queue size but it says constraint inconsistency error.

---

<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:** [May 18, 2021, 12:39am UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/2 "2021-05-18T00:39:47Z")

</div>

_In reply to [Blitzz0418](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087):_

Assuming you meant to write   
arr.sum() with (item==3) **=** = 3\*4, that would be interpreted as there must be exactly 12 elements in arr the element value is 3.

Perhaps you can show some solutions values sets you are looking for.

---

<div class="post-metadata">

**Author:** ![Blitzz0418](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/8c91f0/32.png) [@Blitzz0418](https://verificationacademy.com/forums/u/Blitzz0418)\
**Post date:** [May 18, 2021, 12:53am UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/3 "2021-05-18T00:53:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/2):_  
sure.  
i can have 2, 3, 4, 5 values in an array  
i randomized that using  
foreach arr[i] arr[i] inside {2,3,4,5}  
now i want to have atleast N number of value as 2  
for example,  
arr can have 4 times 2, 3, 4, 5 value as an element.  
what i want to do is restrict the count of 2, 3… to 4 times.  
another condition is they should not be consecutive which is fine.  
i can add another constraint saying  
Arr[i] != Arr[i+1]  
but i am not sure how to put constraint on occurrence of a value/or total number of count on how many times the value shall exists in array ?  
does that makes sense ?

basically i want to have  
Arr = {2,3,4,5,2,4,3,2,3,5,3,2,4,5,4,5,3,4} ; – here i want to limit 2,3,4,5 shall exists atleast 4 times it can be more than that(like 3 and 4 .

---

<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:** [May 18, 2021, 7:04am UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/4 "2021-05-18T07:04:06Z")

</div>

_In reply to [Blitzz0418](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/3):_

That is a lot more information than your original post. Also, you are contradicting what you said first about not repeating_more than 4 times_, and then later you said repeated _at least 4 times_. I’m going to assume you meant at least 4 times which means there needs to be a constraint in the minimum size of the array to be 16. You probably should set an upper bound to the array size as well.

```verilog
class A;
  int N = 4;
  int values[] = {2, 3, 4, 5};
  rand int arr[];
  constraint c {
     arr.size inside {[N*values.size() : 100]};
     foreach(arr[i]) arr[i] inside {values};
     foreach(values[i]) arr.sum() with (int'(item == values[i])) >= N;
  }
endclass

```

---

<div class="post-metadata">

**Author:** ![Blitzz0418](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/8c91f0/32.png) [@Blitzz0418](https://verificationacademy.com/forums/u/Blitzz0418)\
**Post date:** [May 18, 2021, 10:02am UTC](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/5 "2021-05-18T10:02:01Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-number-of-occurrences-in-dynamic-array/39087/4):_

I tried the similar answer but seems it was giving out constraints inconsistency error for sone reason on edaplayground  
let me try with the exact code you gave.  
Thank you
