Dist on Arrays during constrained randomization

Hello,

When we use dist on a array, we use it inside a foreach

for ex:

rand int arr[];
constraint arr_c{
 arr.size() < 100;
 foreach(arr[i])
  arr[i] dist {[0:10]:/20,[20:50]:/60,[70:90]:/20};
}

So in the above code, each element is randomized based on the distribution.i.e. each of them is individually constrained. Is my understanding correct? If so, is there a way to constrain the occurrence of a set of values to occur all across the array with our distribution. Is this possible or individually constraining each element does a implicit constrain on the the array as a whole?

Thank you!

In reply to DK87:

The foreach construct gets unrolled in a constraint expression, so each element gets individually constrained. But a distribution constraint is only a weighting. What statistical difference do you expect from constraining the array as a whole versus each element?

BTW, the dist constraint you wrote is comparable to writing

  arr[I] dist {[0:10]:=9,[20:50]:=74,[70:90]:=17};

make sure you understand the difference between ‘:=’ and ‘:/’.

In reply to dave_59:

Hi Dave,

The use of :/ was intentional as I want the whole range (wanted to assign weight per range and not per element) to have the weight and not each element having the weight.

About my original problem, I wanted to have some sort of dependency between all the elements. Say, with the above “dist”, I would want my whole array to follow the distribution by enforcing some sort of rule that the values of all the elements in the array, follow the weights provided in the “dist”.

For example: if we read out the array of size 100, we would want range (elements) [0:10] occur 20% (around 20 entries), [20:50] occur 60% (around 60 entries) and [70:90] occur 20% (around 20 entries). I am aware that for such a small sample size, exact % is not feasible. But this is the goal that I am after.

Please let me know if there is any issue with my understanding.

In reply to DK87:
I think you have the meaning of ‘:=’ and ‘:/’ reversed.

But in any case, I think the dist constraint does what you want. A simple experiment will confirm if you increases the array size, or increase the number of randomizations.