Hardware interview practice
SystemVerilog dist Constraints Explained
What probability does this distribution assign to x equal to 2?
rand int x;
constraint c { x dist { 0 := 1, [1:3] :/ 6 }; }Answer choices
- A. 1/4
- B. 1/7
- C. 2/7
- D. 6/7
Concise answer
The answer and the key reason
Deeper explanation
Work through the implementation and tradeoffs
The distribution contains four legal values. The singleton entry for zero contributes one weight unit. The slash form on `[1:3]` apportions six units across the members instead of granting six units to every member. Thus each of 1, 2, and 3 carries weight two, making the numerator for `x == 2` equal to two.
Normalizing all weights gives `1 + 2 + 2 + 2 = 7`, so the target event receives two of seven total shares. This is a probability distribution, not a promise about the exact count in a short simulation run. Replacing `:/ 6` with `:= 6` would give each range member weight six, producing `6/19` instead.
What to remember
- `:/` splits a range weight
- Total weight is seven
- Weights describe long-run probability
Practice the complete prompt
