# Constraint on array to get even distribution

**URL:** <https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint-array, even-distribution\
**Created:** [July 1, 2018, 4:58pm UTC](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634 "2018-07-01T16:58:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlsxdx](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mlsxdx](https://verificationacademy.com/forums/u/mlsxdx)\
**Post date:** [July 1, 2018, 4:58pm UTC](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634/1 "2018-07-01T16:58:13Z")

</div>

Hi,

I have some difficulty to constrain an dynamic array to get even distribution on each element.  
Can somebody help me how to write a constraint so that the 8 elements can evenly distribute?

As the code posted,

I got  
'{EE0, EE2, EE3, EE1, EE3, EE0, EE3, EE0}, which is not the goal. The goal is to have 2 EE0, 2 EE1, 2 EE2, 2 EE3.

```verilog
typedef enum bit[1:0] {EE0, EE1, EE2, EE3} ee_e;

class A;

   rand ee_e EEs[];

   constraint EEs_sz {
      EEs.size == 8;
   }

   constraint EEs_even {     
      EEs.xor() == 0;
      EEs.sum with(int'(item)) == 6*2;
      EE0 inside {EEs};
      EE2 inside {EEs};
   }

   function void print_it();
     $display("%p",EEs);
   endfunction // print_it

   function void randomize_it();
      this.randomize();
      void'(print_it());
   endfunction // randomize_it
endclass // A

module tb;
   A a;

   initial begin
      a = new();
      a.randomize_it();
   end
endmodule // tb

```

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

---

<div class="post-metadata">

**Author:** ![javatea](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@javatea](https://verificationacademy.com/forums/u/javatea)\
**Post date:** [July 1, 2018, 6:11pm UTC](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634/2 "2018-07-01T18:11:37Z")

</div>

_In reply to [mlsxdx](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634):_

2 ways to have hard distribution.

```verilog

     typedef enum bit[1:0] {EE0=0, EE1, EE2, EE3} ee_e;

     // 1st method : simple way for your case
     EEs.sum with(int'(item==EE0)) == EEs.size/4;
     EEs.sum with(int'(item==EE1)) == EEs.size/4;
     EEs.sum with(int'(item==EE2)) == EEs.size/4;
     
     // 2nd method : to support more enum type if need
     rand ee_e EEs[];
     rand ee_e helper[4]; 

     foreach(helper[i]) {
       helper[i] == i;
       EEs.sum with(int'(item==helper[i])) == EEs.size/4;
     }

```

---

<div class="post-metadata">

**Author:** ![mlsxdx](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mlsxdx](https://verificationacademy.com/forums/u/mlsxdx)\
**Post date:** [July 1, 2018, 6:54pm UTC](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634/3 "2018-07-01T18:54:22Z")

</div>

_In reply to [javatea](https://verificationacademy.com/forums/t/constraint-on-array-to-get-even-distribution/34634/2):_

Thanks for your reply, javatea.  
The solution provided by you works.

It is good to know that EEs.sum with(int’(item==EE0)) return the number of item == EE0.

So for the code below:  
EEs.sum with(int’(item==EE0)) == EEs.size/4;

the right-hand size EEs.size/4 is 2.  
the left-hand size is the sum() with(int’(item==EE0)), which return the number of element EE0.
