User guide
24-75
SystemVerilog Testbench Constructs
Weighted Distribution
You can use the dist operator to specify a set of values or ranges
of values, where you give each value or range of values a weight,
and the higher the weight, the more likely VCS is to assign that value
to the random variable.
If we modify the previous example as follows:
program prog;
class myclass;
rand int int1;
constraint c1 {int1 dist {[1:2] := 1, [6:7] :/ 5, 9
:=10};}
endclass
myclass mc = new;
int stats [1:9];
int i;
initial begin
for(i = 1; i < 10; i++) stats[i] = 0;
repeat (1700)
if(mc.randomize()==1) begin
stats[mc.int1]++;
//$display("int1=%0d",mc.int1);
end
for(i = 1; i < 10; i++)
$display("stats[%d] = %d", i, stats[i]);
end
endprogram
constraint c1 in class myclass is now:
constraint c1 {int1 dist {[1:2] := 1, [6:7] :/ 5, 9
:=10};}
Constraint C1 now specifies the possible values of int1 are as follows: