14 Rand and Dist (Constraint block)
dist, the distribution operator, is used in constraint blocks to specify the distribution of results. An introduction.
dist is an operator that is used in constraint blocks to control the probability of a particular expression matching. (In the general case this means controlling the probability that a rand variable will be randomized to a specific value.)
ex: I will roll a game die, let the value be 6
50% of the time and the other 50% of the time, spread the probability across the other values (1,2,3,4,5).
game_die dist { 6 :/ 50, [1:5] :/50};
dist, to specify distribution of constraint results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module dice_game;
// distributions (dist) can be used in constraints for specifying sets of weighted values
// which control the distribution of results
// distribution set - comma separated list of values (or expressions or ranges)
class dice;
rand int value; //random variable
int stats[1:6] = {0,0,0,0,0,0};
// constraint six_sides { value>=1 && value<=6; }
constraint fixed_odds{ value dist {6,5,4,3,2,1}; }
/* constraint fixed_odds{ value dist {
6:/50, //50/(50+1+1+1+1+1)
5:/1, // 1/(50+1+1+1+1+1)
4:/1, // 1/(50+1+1+1+1+1)
3:/1, // 1/(50+1+1+1+1+1)
2:/1, // 1/(50+1+1+1+1+1)
1:/1 // 1/(50+1+1+1+1+1)
}; }
constraint fixed_odds{ value dist {
6:=50, //50/(50+1+1+1+1+1)
5:=1, // 1/(50+1+1+1+1+1)
4:=1, // 1/(50+1+1+1+1+1)
3:=1, // 1/(50+1+1+1+1+1)
2:=1, // 1/(50+1+1+1+1+1)
1:=1 // 1/(50+1+1+1+1+1)
}; }
*/
// constraint fixed_odds{ value dist {[4:6]:/1, 3:/1, 2:/1, 1:/1}; }
// constraint fixed_odds{ value dist {[4:6]:=1, 3:/1, 2:/1, 1:/1}; }
// constraint fixed_odds{ value dist {[6:4]:=1, 3:/1, 2:/1, 1:/1}; }// BAD RANGE [low:high]. error
function void post_randomize();
$display(" dice roll is now = %0d", value);
stats[value] = stats[value]+1;
endfunction
function void show_stats();
foreach (stats[iii]) begin
$display("%3d : %3d of %3d : %2.2f%",
iii, stats[iii], stats.sum(), (real'(stats[iii])/real'(stats.sum()))*100);
end
endfunction
endclass
initial begin
dice my_dice;
my_dice = new();
repeat (100) begin
assert (my_dice.randomize()) else begin
$display(" ERROR: Mister SVS, randomization failed, miserably.");
$fatal(1);
end
end
my_dice.show_stats();
end
endmodule // dice_game
.
//run command
1
irun -sv dice_roll.sv