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;
randbit [0:2] Var;
constraint limit_c { Var < 4;}
endclass
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;
randcbit [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
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;
randcbit [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
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;
randcbit [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: