Multiple dist constraints

Hi Forum,

Consider the following constraint

class Dist;

 rand bit [3:0] dst;

 constraint DIST1 { dst dist { 5:=1 , [12:15]:=1 }; }

 //constraint DIST2 { dst dist { 10:=1 }; }

 //constraint DIST3 { dst dist { 15:=2 }; }

endclass

On randomizing an object of Dist, 20 times I observe ::

(1) On uncommenting DIST2 there is a constraint failure as the value of 10 isn’t legal as per DIST1

(2) On uncommenting DIST3 I observe that dst always takes value as 15 ( all 20 times )

[Q] Shouldn’t dst also be randomized ( with lesser probability ) to value of 5 / 12 / 13 / 14 ?

Thanks

A dist is a hard constraint that encompasses all the values with non-zero weighted in the bracketed set. As soon as you introduce the DIST2 constraint, you’re faced with a conflicting set of values. This is essentially the same as applying an inside constraint; the result is the intersection of the values in the two lists. If instead of adding the DIST2 constraint, you have the DIST1 and DIST3 values, the intersection of these values is only 15.

Thanks Dave.

Seeking your comments on calculation of distribution weights in following case

1800-2023 says

If a value occurs in multiple items within a single distribution,
then the weights allocated to that value are additive.
For example:

x dist { [100:102]:=1, 101:= 1 }; // value 101 occurs twice

Weight associated to value of x = 101 would be additive i.e 1+1 i.e 2

Consider a hypothetical scenario where user has the following inline constraint for class Dist

Assuming only DIST1 exists in the class definition.

Dist d;

initial begin
  d = new();
  // Different weights specified via inline constraint 
  if( !d.randomize() with { dst dist { 5:=2,[12:15]:=3 };} )
      $error(..);
end

(A) What is the individual weight for dst equal to 5 / 12 / 13 / 14 / 15 ? Will weights still be additive ?

I believe LRM doesn’t mention a scenario where the weights are different via inline constraint or via separate constraint block.

Applying multiple distributions to the same random variable is asking for trouble. I believe that the weights are additive, regardless of whether the distribution is specified in multiple constraints blocks or in-line. Moreover, it’s important to remember that each value must have a non-zero weight in every distribution; otherwise, it’s considered a hard constraint that prevents that value from being part of the solution set.

Applying multiple distributions to the same random variable is asking for trouble

Yes, I do agree that a user shouldn’t do it purposely

A final question from my side

dist constraint is essentially the same as applying an inside constraint with an addition of weights

Does this mean one can use an unpacked array variable in distribution set ?

rand bit [2:0] a;
     bit [2:0] a_q[$]; // non-random variable 


// Assuming queue 'a_q' is always non-empty during call to randomize 
constraint dist_unpack { a dist { a_q }; } // default weight of 1 per element
// Above constraint is essentially an inside constraint 

I am aware that an inside operator does allow an unpacked array variable in its range_list

LRM says

The distribution set is a comma set list of integral or real expressions and ranges

As per my understanding an unpacked array variable as a whole isn’t an integral expression,

an element of unpacked array would be an integral expression ( assuming it’s leaf element is integral type )

Unfortunately, the syntax doesn’t support that. However, that would be a great enhancement request to align the two constructs.