Tutorials

RANDOM VARIABLES

Random Varible Declaration:

Variables declared as rand or randc are only randomized due to call of randomize() function. All other varibles are considered as state variables.

EXAMPLE:
class ex_8;
rand [3:0] var1;
randc [3:0] var2;
rand integer var3;
endclass

Fixed arrays, dynamic arrays, associative arrays and queues can be declared as rand or randc. All their elements are treated as random. Individual array elements can also be constrained,in this case, index expression must be constant. For dynamic arrays, the size of the array length can be constrained. Non integer data types like shortreal, real and realtime are not allowed for random variable declaration.

Rand Modifier :

Variables declared with rand keyword are standard random variables. When there are no other control on distrubution, these variables are uniformly distributed across valid values.

EXAMPLE:
class rand_cl;
rand bit [0:2] Var;
constraint limit_c { Var < 4;}
endclass

program rand_p_9;
rand_cl obj;
integer count_0, count_1, count_2, count_3;
initial
begin
obj = new();
count_0 = 0;count_1 = 0;count_2 = 0;count_3 = 0;
repeat(100000)
begin
void'(obj.randomize());
if( obj.Var == 0) count_0 ++;
else if( obj.Var == 1) count_1 ++;
else if( obj.Var == 2) count_2 ++;
else if( obj.Var == 3) count_3 ++;
end
\$display(" count_0 = %0d , count_1 = %0d, count_2 = %0d, count_3 = %0d ",count_0, count_1, count_2, count_3);
end
endprogram

RESULTS:

count_0 = 25046 , count_1 = 24940, count_2 = 24969, count_3 = 25045

Simulation results show that the rand variable is distrubuted uniformly.

Randc Modifier :

Variables declared as randc are random cyclic that randomly iterates over all the values in the range and no value is repeated with in an iteration until every possible value has been assigned. But Iteration sequences are won't be same. Bit and enumerated types can be randc variables. To reduce memory requirements, implementations can impose a limit on maximum size of a randc variable, it should be not be more than 8 bits.

EXAMPLE:
class rand_c;
randc bit [1:0] Var;
endclass

program rand_cp_10;
rand_c obj=new();
initial
for(int i=0;i<20;i++)
begin
void'(obj.randomize());
\$write("%0d_",obj.Var);
if(i%4==3)
\$display("");
end
endprogram

RESULTS:

# 0_3_1_2_
# 3_0_2_1_
# 0_3_1_2_
# 0_1_2_3_
# 3_0_2_1_

The permutation sequence for any given randc variable is recomputed whenever the constraints changes on that variable or when none of the remaining values in the permutation can satisfy the constraints.

EXAMPLE:
class rand_c;
randc bit [2:0] Var;
integer MIN = 4;
constraint C { Var < MIN ;}
endclass

program rand_cp_11;
rand_c obj=new();
initial
for(int i=0;i<20;i++)
begin
obj.MIN = 4;
if(i>12)
obj.MIN=7;
void'(obj.randomize());
if(i==12)
\$display(" CONSTRAINT CHANGED ");
\$write("%0d_",obj.Var);
if((i%4==3))
\$display("");
end
endprogram

RESULTS:

0_2_3_1_
0_1_3_2_
3_2_0_1_
CONSTRAINT CHANGED
0_1_4_2_
6_5_3_0_

Permutation sequence is computed on every call of new() function. So if randc variables won't behave like random cyclic, if new() is called for every randomization. In the following example variable Var is not behaving like random cyclic.

EXAMPLE:
class rand_c;
randc bit [1:0]Var;
endclass

program rand_cp_12;
rand_c obj=new();
initial
for(int i=0;i<20;i++)
begin
obj=new();
void'(obj.randomize());
\$write("%0d_",obj.Var);
if(i%4==3)
\$display("");
end
endprogram
RESULTS:

# 1_3_1_2_
# 3_2_2_1_
# 2_0_0_0_
# 3_3_1_0_
# 3_0_1_0_

 << PREVIOUS PAGE TOP NEXT PAGE >>