Weighted Distribution in System Verilog
In short: The dist keyword in a constraint makes some random values appear more often than others. Two operators set the weights. := gives the stated weight to each value in a range, so a wider range gets more total probability. :/ gives the stated weight to the range as a whole, split evenly across its values. Use weighted distributions to steer stimulus toward corner cases that plain random rarely hits.
In constrained random testing, a rare scenario can stay uncovered even after many runs, leaving holes in functional coverage. A weighted distribution nudges the solver to pick certain values more often, so those corner cases show up sooner. The trick is knowing how the two dist operators split the weight.
A simple way to picture it
Think of a raffle where you buy tickets. With :=, every name in a group gets its own full book of tickets, so a bigger group ends up with more tickets overall. With :/, the group shares one book of tickets, split evenly, so the group total is fixed no matter how many names are in it. Same idea, two ways to hand out the tickets.
The := operator: weight per value
With :=, the weight applies to each value. If a range holds three values and you write [1:3] := 60, each of 1, 2, and 3 gets weight 60, so the range contributes 180 in total.
class dist_each;
rand bit [1:0] a; // values 0..3
constraint c_a {
a dist { 0 := 40, [1:3] := 60 };
}
endclass
// weights: 0 -> 40, 1 -> 60, 2 -> 60, 3 -> 60 (total 220)
// P(a==0) = 40/220, P(a==1)=P(a==2)=P(a==3) = 60/220Because each value in the range carries the full 60, the wider range pulls more of the total probability.
The :/ operator: weight per range
With :/, the weight applies to the whole range, then splits evenly among its values. Writing [1:3] :/ 60 gives the range 60 total, so each of 1, 2, and 3 gets 20.
class dist_range;
rand bit [1:0] b; // values 0..3
constraint c_b {
b dist { 0 :/ 40, [1:3] :/ 60 };
}
endclass
// weights: 0 -> 40, range[1:3] -> 60 shared => 20 each (total 100)
// P(b==0) = 40/100, P(b==1)=P(b==2)=P(b==3) = 20/100The same ranges, two very different results
| Value | With := (weight each) | With :/ (weight shared) |
|---|---|---|
| 0 | 40 / 220 | 40 / 100 |
| 1 | 60 / 220 | 20 / 100 |
| 2 | 60 / 220 | 20 / 100 |
| 3 | 60 / 220 | 20 / 100 |
Same numbers, same ranges, but := gives each in-range value a much higher chance than :/ does. Choosing the wrong operator is a common cause of a distribution that looks off.
Measuring the distribution
You can confirm the shape by randomizing many times and counting how often each value appears. Over 10000 runs the counts track the expected fractions closely.
module tb;
initial begin
dist_each obj = new();
int count[4];
repeat (10000) begin
if (!obj.randomize()) $error("randomize failed");
count[obj.a]++;
end
foreach (count[i])
$display("a==%0d : %0d hits (~%0.3f)", i, count[i], count[i]/10000.0);
end
endmoduleExpected output, in plain words
For the := class, value 0 appears about 40 out of 220 of the time, roughly 18 percent, while each of 1, 2, and 3 appears about 27 percent. For the :/ class, value 0 appears about 40 percent and each of 1, 2, and 3 about 20 percent. Counts vary a little by seed but track these fractions.
Note: these outputs describe what the code is written to produce from a read of the IEEE 1800 LRM. Confirm on your own simulator or EDA Playground.
A practical example: bias toward a corner
A common real use is biasing a length field so short and maximum lengths, which often expose bugs, appear more often than the middle values.
class packet;
rand bit [7:0] len; // 0..255
constraint c_len {
len dist { 0 := 20, [1:254] :/ 30, 255 := 20 };
}
endclass
// 0 and 255 each get a heavy per-value weight;
// the whole middle range shares 30, so any single mid value is rareWhen to use weighted distributions
- You want to hit corner cases, such as minimum and maximum values, more often than plain random gives.
- You are filling coverage holes that random testing keeps missing.
- You want realistic traffic, such as mostly small packets with a few large ones.
- You need to bias one field while leaving others fully random.
Common mistakes to avoid
- Mixing up := and :/: per value versus per range changes the odds a lot. Pick the one that matches your intent.
- Weights that sum oddly: weights are relative, not percentages, so read them as shares of the total, not as fixed percents.
- Zero weight expecting rare, not never: a value with weight zero is never chosen, not just rare.
- Forgetting dist needs a rand variable: the distribution only applies inside a constraint on a random variable.
dist vs randcase weights
Both use weights, but dist biases the value a random variable takes inside the solver, while randcase biases which branch of code runs. Use dist to shape data such as a length or address; use randcase to pick an action. We compare branch and sequence selection in randcase vs randsequence.
For the randomization entry points, see randomize vs std::randomize. To toggle which rules apply, read enable and disable constraint. More is in our SystemVerilog tutorials.
Frequently asked questions
What is a weighted distribution in SystemVerilog?
It uses the dist keyword in a constraint to make some random values appear more often than others. You set weights with the := and :/ operators so the solver biases the values it picks.
What is the difference between := and :/ in dist?
The := operator gives the stated weight to each value in a range, so a wider range gets more total probability. The :/ operator gives the weight to the whole range and splits it evenly across the values.
When should I use := versus :/?
Use := when you want every value in a range to be equally likely and as likely as a single listed value. Use :/ when you want a range to have a fixed total share no matter how many values it holds.
Why use weighted distributions at all?
To hit corner cases that plain random rarely reaches, such as minimum and maximum values, and to fill functional coverage holes faster than uniform random testing.
Are dist weights percentages?
No. Weights are relative shares. The chance of a value is its weight divided by the sum of all weights, so read them as ratios, not fixed percentages.
What is the difference between dist and randcase weights?
dist biases the value a random variable takes inside the solver. randcase biases which branch of code runs. Use dist to shape data and randcase to pick an action.


temp_q = a.find with (item == i);
For i = 0, queue is empty. And it gets filled with all 0’s.
For i =1, queue contains elements with all 0’s. After above array locator method execution isn’t queue will contain elements of both 0 and 1?
Shouldn’t temp_q all elements be deleted after display statement?
Did you try that logic? you should tweak the example given with your values accordingly and understand how it works. The queue doesn’t work that way you need to use a queue.delete() method to delete the elements.
Please refer to this once
https://theartofverification.com/different-array-types-and-queues-in-system-verilog/
Thank you