# Systemverilog constraints

**URL:** <https://verificationacademy.com/forums/t/systemverilog-constraints/43330>\
**Category:** SystemVerilog\
**Tags:** constraint-randomization, systemverilog-constraint\
**Created:** [December 20, 2023, 12:09pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330 "2023-12-20T12:09:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgufran365](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mgufran365/32/1093_2.png) [@mgufran365](https://verificationacademy.com/forums/u/mgufran365)\
**Post date:** [December 20, 2023, 12:09pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/1 "2023-12-20T12:09:37Z")

</div>

In the below transaction class definition, write your own logic in the space provided for you  
to meet the following requirements. –  
a. The dynamic array should contain 20 items.  
b. Out of 20 items, the values of 3 items should be equal to 5 at random positions.  
c. The values of 5 items must be 10 at random positions  
d. The values of 8 items must be 15 and the remaining 4 items must be 20.  
Note : Please don’t use distribution constraint, as it will not really meet this requirement &  
post randomize method

---

<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:** [December 20, 2023, 8:10pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/2 "2023-12-20T20:10:55Z")

</div>

I will give you a hint. Requirement b can be stated by

```auto
constraint b { array.sum() with {int'(item==5)} == 3; }

```

---

<div class="post-metadata">

**Author:** ![mgufran365](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mgufran365/32/1093_2.png) [@mgufran365](https://verificationacademy.com/forums/u/mgufran365)\
**Post date:** [December 21, 2023, 5:50am UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/3 "2023-12-21T05:50:26Z")

</div>

```auto
class packet;
  int array[];
  constraint s{array.size==20;}
  constraint b { array.sum() with (int'(item==5)) == 3; }
  constraint c { array.sum() with (int'(item==10)) == 5; }
  constraint d { array.sum() with (int'(item==15)) == 8; }
  constraint e { array.sum() with (int'(item==20)) == 4; }
endclass

module tb;
  packet h;
  initial 
    begin 
      h=new();
      assert(h.randomize);
      $display("array=%p",h);
      end
endmodule

```

simulation result getting  
array=‘{array:’{}}

---

<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:** [December 21, 2023, 6:23am UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/4 "2023-12-21T06:23:21Z")

</div>

You need to declare `array` with `rand`

```auto
rand int array[];

```

---

<div class="post-metadata">

**Author:** ![nimrodw1](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/nimrodw1/32/1202_2.png) [@nimrodw1](https://verificationacademy.com/forums/u/nimrodw1)\
**Post date:** [January 16, 2024, 5:22pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/5 "2024-01-16T17:22:57Z")

</div>

> [@dave\_59](#):
>
> `constraint b { array.sum() with {int'(item==5)} == 3; }`

Hi,

Why is it ==3 and not ==15? (15 being the sum of 3 elements equal to 5)  
Doesn’t sum() return the sum of array elements?

---

<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:** [January 16, 2024, 5:36pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraints/43330/6 "2024-01-16T17:36:14Z")

</div>

The requirement is:

> [@mgufran365](#):
>
> b. Out of 20 items, the values of 3 items should be equal to 5 at random positions.

We are constraining the _count of items_ with the value 5, not the the sum of the items. The `sum()` method returns the sum of the iterative expressions inside the `with ()` clause.
