# Constrain the number of occurrences in an array

**URL:** <https://verificationacademy.com/forums/t/constrain-the-number-of-occurrences-in-an-array/33293>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint, array-randomization\
**Created:** [July 12, 2017, 5:24pm UTC](https://verificationacademy.com/forums/t/constrain-the-number-of-occurrences-in-an-array/33293 "2017-07-12T17:24:13Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [November 8, 2020, 7:35am UTC](https://verificationacademy.com/forums/t/constrain-the-number-of-occurrences-in-an-array/33293/8 "2020-11-08T07:35:44Z")

</div>

_In reply to [Juhi\_Patel](https://verificationacademy.com/forums/t/constrain-the-number-of-occurrences-in-an-array/33293/7):_

Reason is your statement

```verilog

  c_inst[i]= new(); 

```

There is no element of array c\_inst yet .

Change your pre\_randomize() to ::

```verilog

function void pre_randomize();
    c_inst = new[10]; // 10 elements at default value 'null' 
    foreach(c_inst[i])  
    begin
      c_inst[i]= new();
      c_inst[i].randomize(); // Not Needed !!
    end
  endfunction

```

---

_[View the full topic](https://verificationacademy.com/forums/t/constrain-the-number-of-occurrences-in-an-array/33293)._
