# Constraint Randomization

**URL:** <https://verificationacademy.com/forums/t/constraint-randomization/36876>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraints\
**Created:** [December 2, 2019, 7:38am UTC](https://verificationacademy.com/forums/t/constraint-randomization/36876 "2019-12-02T07:38:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rre\_dortmund](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@rre\_dortmund](https://verificationacademy.com/forums/u/rre_dortmund)\
**Post date:** [December 2, 2019, 7:38am UTC](https://verificationacademy.com/forums/t/constraint-randomization/36876/1 "2019-12-02T07:38:23Z")

</div>

Hi All,

I need help in writing constraint.

```verilog
class foo;

rand bit [31:0] numbers [5];

//constraint requirements are that array elements are unique and the numbers should be non consecutive numbers as well. Numbers are between 0 -100
// Valid Solution can be: 0 2 6 9 11. 
// While if the solution is 0, 2, 3 , 6, 9 May be unique, this wont be a valid solution.

endclass:foo

```

---

<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 2, 2019, 5:02pm UTC](https://verificationacademy.com/forums/t/constraint-randomization/36876/2 "2019-12-02T17:02:48Z")

</div>

_In reply to [rre\_dortmund](https://verificationacademy.com/forums/t/constraint-randomization/36876):_

That is three constraints

```verilog
constraint range { foreach (numbers[i]) numbers[I] inside {[0:10]}; }
constraint uniqu { unique {numbers}; }
constraint nonco { foreach (numbers[i]) foreach (numbers[j])
                        numbers[i] - numbers[j] != 1; }

```

You might want to optimize this to remove redundant or unnecessary constraints. (like when i==j, and comparing [j][i] versus [i][j])
