# Constraint to cause a value to be within a set defined in a dynamic array

**URL:** <https://verificationacademy.com/forums/t/constraint-to-cause-a-value-to-be-within-a-set-defined-in-a-dynamic-array/36940>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, UVM, Dynamic-Array, constraints\
**Created:** [December 18, 2019, 8:49pm UTC](https://verificationacademy.com/forums/t/constraint-to-cause-a-value-to-be-within-a-set-defined-in-a-dynamic-array/36940 "2019-12-18T20:49:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul\_mckechnie](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@paul\_mckechnie](https://verificationacademy.com/forums/u/paul_mckechnie)\
**Post date:** [December 18, 2019, 8:49pm UTC](https://verificationacademy.com/forums/t/constraint-to-cause-a-value-to-be-within-a-set-defined-in-a-dynamic-array/36940/1 "2019-12-18T20:49:55Z")

</div>

I am trying to write a constraint that will cause a variable to be selected from a set of predefined values. This would only occur when another bit is set to true so I would like to place it within an implication. If valid\_transaction is not true then any value can be picked.

I have sketched out the following pseudo-code.

byte set\_of\_values [6] = {'h3, 'h6, 'h7, 'h9};

rand bit valid\_transaction;  
rand byte my\_value;

constraint valid\_c {  
valid\_transaction → my\_value inside {set\_of\_values};  
}

The part that I can’t get to compile is the “my\_value inside {set\_of\_values}”. Is there a valid syntax to cause my\_value to take on one of the values in set\_of\_values? Or do I need to take a slightly different approach?

Thanks.

---

<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 18, 2019, 10:47pm UTC](https://verificationacademy.com/forums/t/constraint-to-cause-a-value-to-be-within-a-set-defined-in-a-dynamic-array/36940/2 "2019-12-18T22:47:10Z")

</div>

_In reply to [paul\_mckechnie](https://verificationacademy.com/forums/t/constraint-to-cause-a-value-to-be-within-a-set-defined-in-a-dynamic-array/36940):_

You are declaring set\_of\_values with a fixed size of 6, yet you are trying to initialize it with an array of 4 values. You need to change the size of something, or you can declare it as a dynamic array, set\_of\_values. When you reference the array, do not use any 's.

Also realize that valid\_transaction only has a 4/256 = 1.6% chance of being set.  
[https://verificationacademy.com/forums/systemverilog/constrain-random-variable-result-enumerated-type#reply-48832](https://verificationacademy.com/forums/systemverilog/constrain-random-variable-result-enumerated-type#reply-48832)
