randcase is a case statement that randomly selects one of its branches. The randcase item expressions are non-negative integral values that constitute the branch weights. An item weight divided by the sum of all weights gives the probability of taking that branch. Randcase can be used in class are modules. The randcase weights can be arbitrary expressions, not just constants.
EXAMPLE: randcase 3 : x = 1;
1 : x = 2;
4 : x = 3;
endcase
The sum of all weights is 8; therefore, the probability of taking the first branch is 0.375, the probability of taking the second is 0.125, and the probability of taking the third is 0.5. If a branch specifies a zero weight, then that branch is not taken. The sum of all weights (SUM) is computed (negative values contribute a zero weight). If SUM is zero or exceeds (2*32-1), no branch is taken. Each call to randcae statement will return a random number in the range from 0 to SUM. $urandom_range(0,SUM) is used to generate a random number. As the random numbers are generated using $urandom are thread stable, randcase also exhibit random stability.
EXAMPLE: module rand_case_70;
integer x;
integer cnt_1,cnt_2,cnt_3;
initial begin cnt_1 = 0;cnt_2=0;cnt_3 = 0;
repeat(100000)
begin randcase 3 : x = 1;
1 : x = 2;
4 : x = 3;
endcase if(x == 1)
cnt_1++;
elseif(x == 2)
cnt_2++;
elseif(x ==3)
cnt_3++;
end $display("count_1 =%0d count_2 =%0d count_3 =%0d ",cnt_1,cnt_2,cnt_3);
$display("Probability of count_1 =%0f count_2 =%0f count_3 =%0f ",(cnt_1/100000.0),(cnt_2/100000.0),(cnt_3/100000.0));
end endmodule
RESULTS:
# count_1 =37578 count_2 =12643 count_3 =49779 # Probability of count_1 =0.375780 count_2 =0.126430 count_3 =0.497790