Understanding a Bug in the Distribution Constraint

Hi,
Could anyone help me understand why the following code has a bug?

class foo
  rand byte bar;
  constraint c_bar_dist{
    bar dist {[0:99] := 5, 100 := 95};
  }
endclass

What error are you getting?

If you add a semi-colon after ‘class foo’, your code works fine.

1 Like

Thank you for your response. I just came across a post on LinkedIn that said that this code has a logical bug (rather than a syntax error), wherein the probabilities exceed 100. And I couldn’t understand why. That’s what I was seeking an explanation for. Sorry for not clarifying this earlier.

“Bugs” only exist when there is a difference in actual versus expected/required behavior. You never mentioned what you are expecting.

I can assume you were expecting a distribution with the value 100 occurring 95% of the time, and the other values in the range 0-99 occurring 5% of the time. If that is the case, you needed to specify the weight using [0:99]:/5 not [0:99]:=5.

With [0:99]:=5, a weight of 5 gets placed on each value so the total of all the weights is 595. Then the probability of the value 100 is 95/(500+95)=16%.

With [0:99]:/5, the weight of 5 get applied to the range as a whole. Then the probability of the value 100 is 95/(5+95)=95%.

Note the IEEE 1800-2023 SystemVerilog LRM has clarified the description of the :/ weight calculation showing the weight applies to the range as a whole, not divided into each value in the range.

2 Likes

Thanks a lot for the clarification. That is what I was expecting.