randc (random-cyclic) variables, when randomized, will cycle through all possible values for their data type (observing any constraints) before repeating values.
Here we use an int named value
to represent a playing die. It is constrained to have a value from 1 through 6. When we change its declaration from rand to randc, we see that every value of the die will be rolled before we get a duplicate.
Then we add a new constraint to the die (to only have odd rolls).
randc (random-cyclic) versus rand
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
| module dice_game;
class dice;
//rand int value; //random variable
randc int value; //random-cyclic variable (cycles thru all values before repeating)
constraint six_sides { value>=1 && value<=6; }
constraint odds { (value%2)==1; }
function void post_randomize();
$display(" dice roll is now = %0d", value);
endfunction
endclass
initial begin
dice my_dice;
my_dice = new();
//my_dice.constraint_mode(0);
repeat (10) begin
assert (my_dice.randomize()) else $fatal(" ERROR: Mister SVS, randomization failed, miserably.");
end
end
endmodule // dice_game
|