# Constraint randomization of an array

**URL:** <https://verificationacademy.com/forums/t/constraint-randomization-of-an-array/28283>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [January 7, 2013, 11:20pm UTC](https://verificationacademy.com/forums/t/constraint-randomization-of-an-array/28283 "2013-01-07T23:20:27Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![gnich](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/gnich/32/3311_2.png) [@gnich](https://verificationacademy.com/forums/u/gnich)\
**Post date:** [September 2, 2016, 4:01pm UTC](https://verificationacademy.com/forums/t/constraint-randomization-of-an-array/28283/12 "2016-09-02T16:01:13Z")

</div>

_In reply to [Jonathan\_Alvarez](https://verificationacademy.com/forums/t/constraint-randomization-of-an-array/28283/11):_

I’m using a Cadence simulator.  
I get a constraint error using vf.sum (32’(item)) == 2048.  
I tried vf.sum (item+32’b0) == 2048, but I get a constraint error.

Here’s my code.

````verilog

``` verilog
class rand_vf_queues extends uvm_object;

   rand bit [7:0] vf_size;
   rand bit [11:0] vf[];
   
   constraint c_num_vf {vf_size == 248;} 

   constraint c_vf_queues {
			 vf.size == 248;
			 vf.sum == 2048;
                         foreach (vf[i]) {
			   vf[i] inside {[0:2048]};
                         }
                        
   }

 
   function new (string name = "");
   endfunction // new

   function void post_randomize();
      int sum_vf = 0;
      `uvm_info(get_type_name(), $psprintf("vf_size = %0d, total_qctl_queues = %0d",vf_size,total_qctl_queues), UVM_LOW)
      for(int i=0; i<vf_size; i++) begin
        `uvm_info(get_type_name(), $psprintf("VF[%0d] = %0d",i,vf[i]), UVM_LOW)
	 sum_vf = sum_vf + vf[i];
      end
      `uvm_info(get_type_name(), $psprintf("VF.SUM() = %0d, sum_vf = %0d",vf.sum(),sum_vf), UVM_LOW)
   endfunction        

endclass // rand_vf_queues

````

````auto

> Here's partial display results showing the end of the array values and sum_vf. Notice VF.SUM() has maxed out to 2048 but the actual sum_vf is much larger.

``` verilog
[0ns] reporter: VF[242] = 1455
[0ns] reporter: VF[243] = 19
[0ns] reporter: VF[244] = 469
[0ns] reporter: VF[245] = 353
[0ns] reporter: VF[246] = 946
[0ns] reporter: VF[247] = 2038
[0ns] reporter: VF.SUM() = 2048, sum_vf = 256000

````

What I would like to do is constrain all the values from 0-2048 and have the total array sum = 2048 and I want the possibility to have only a couple of the vf[i] to have a couple of indexes = \> 0 and all the other indexes = 0.  
i.e. vf[0] = 2000, vf[230] = 48, all other indexes will = 0.

---

_[View the full topic](https://verificationacademy.com/forums/t/constraint-randomization-of-an-array/28283)._
