# Creating unique values in array within a range of values

**URL:** <https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, unique, sytem-verilog\
**Created:** [June 8, 2021, 10:32pm UTC](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162 "2021-06-08T22:32:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aashishs2603](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@aashishs2603](https://verificationacademy.com/forums/u/aashishs2603)\
**Post date:** [June 8, 2021, 10:32pm UTC](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162/1 "2021-06-08T22:32:53Z")

</div>

Hello

I want to create unique values in an array. I understand SV provides unique construct to do such a thing. However I want to constraint those array values further between 2 values.

```auto
class Nibbleset
   rand bit [3:0] nibbles[10];
   constraint uniq {unique {nibbles};}
endclass

```

Problem here is that values are constrained by size of each entry which is 4 bit. So each value can be between 0 and 15. if I want say unsized array or queue with randomized size and unique values between integer a and b, is there construct which comes with unique ?

```auto
rand logic [4:0] d_arr[$];
  constraint uniq {d_arr.size < 7;
                   unique {d_array}}

```

Something in 2nd code to say please choose value between 7 and 30 ?

---

<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:** [June 8, 2021, 11:40pm UTC](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162/2 "2021-06-08T23:40:51Z")

</div>

_In reply to [aashishs2603](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162):_

You can use a **foreach** iterative constraint and the **inside** operator to constrain each element in a range. You should also use **inside** constrain the size of the array.

```verilog
rand logic [4:0] d_arr[$];
  constraint uniq {d_arr.size inside {[1:7]};
                   unique {d_arr}};
                   foreach (d_arr[i]) d_arr[i] inside {[7:30]};
  }

```

---

<div class="post-metadata">

**Author:** ![aashishs2603](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@aashishs2603](https://verificationacademy.com/forums/u/aashishs2603)\
**Post date:** [June 9, 2021, 12:07am UTC](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162/3 "2021-06-09T00:07:41Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/creating-unique-values-in-array-within-a-range-of-values/39162/2):_

Thanks Dave. I used foreach and it worked. However I was thinking since unique was introduced to remove foreach from constraint is there any other method without foreach?

Also there is construct which I tried which should exclude values but it did not work. I still values being included

```auto
rand logic [4:0] d_arr[$];
const bit [4:0] excludes {0,1,2};
  constraint uniq {d_arr.size inside {1:7};
                   unique {d_array,excludes}};
                   
}

```
