Constraint Randomization

constraint c2 { A.sum() with (int’(item)) < 1000; }

In the above constraint used in LRM , what is the purpose of item in int’(item)?

It is static casting the of the elements of A**, into int** type.

The constraint is used to generate an array A, such that the sum of all the elements of A is less than 1000.

For example, you are randomizing a bit array A, then adding elements shall have the sum in bit datatype only (Henceforth, the sum shall never be ). So, in order to make this addition sensible, the elements are first casted to int type and then used for constraint.

rand bit A[]; // note the bit datatype. Arbitrary elements can be {0,1,0,0,1} (binary), but sum is 2 (decimal).
rand int unsigned a_size;
constraint c2 { A.sum() with (int'(item)) < 1000;  // convert sum to int type
                // A.sum() with (item) < 1000; // sum goes either 0 or 1
                A.size = a_size;
                solve a_size before A;}

Refer Static Cast for more information.

In reply to sharvil111:

But what is the use of ‘item’ their ? I have replaced X instead of item then it is not working as the same for ‘item’.

In reply to Huzefa Halolwala:

item refers to elements of array

In reply to Huzefa Halolwala:

In reply to sharvil111:
But what is the use of ‘item’ their ? I have replaced X instead of item then it is not working as the same for ‘item’.

Referring to IEEE 1800-2012, Section 7.12:

Array manipulation methods iterate over the array elements, which are then used to evaluate the expression specified by the with clause. The iterator_argument optionally specifies the name of the variable used by the with expression to designate the element of the array at each iteration. If it is not specified, the name item is used by default.

As stated by arunj, the term ‘item’ refers to the individual elements in array. It is the default name given.

You can use any variable instead. But, user have to state/declare that variable (X** in your case) while using the array method, then it shall compile fine.

A.sum(X) with (int'(X)) < 1000; // notice X in sum(X)

In reply to sharvil111:

Thank you sharvil111