Built-in method randomize() not only used for randomization, it can be used as checker. When randomize() method is called by passing null, randomize() method behaves as checker instead of random generator. It evaluates all the constraints and returns the status. This is true for both scope randomization function and class randomization function. When a randomize() method is called, first RNG assigns values to random varibles and then solver checks the constraints. When randomize(null) is called, it wont call the RNG to assign values to random variables, it just solves constraints.
EXAMPLE: class Eth_rx;
randinteger Pkt_len;
randinteger Var;
constraint var_c { Var < 1518 ;Var > 64 ;}
endclass
program Eth_25;
Eth_rx rx = new();
initial begin rx.Pkt_len = 32;
rx.Var = 871;
if(rx.randomize(null))
$display(" VALID PKT IS RECIVED ");
else $display(" INVALID PKT IS RECIVED ");
end endprogram
RESULTS:
# VALID PKT IS RECIVED
Constraints can be written without having random varibles in expressions. If there is any constraint on state variables and they are dynamically changed, and if you want to make sure that these dynamic changes should satisfy the constraint, use randomize check to make sure that relation is satisfied.
In the following example, MIN and MAX are dynamically controllable state variables. Constraint checker_c fails when MIn = 50 and MAX = 10.
EXAMPLE: class Base;
randinteger Var;
integer MIN,MAX;
constraint randge_r { Var < MAX ; Var > MIN ;}
constraint checker_c{ MIN < MAX ;} // This checks that these dynamic variables are valid
task set (integer MIN,integer MAX);
this.MIN = MIN;
this.MAX = MAX;
$display( " SET : MIN = %0d , MAX = %0d ",MIN,MAX);
endtask endclass
program inhe_26;
Base obj;
initial begin obj = new();
obj.set(0,100) ;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
obj.set(50,10) ;
for(int i=0 ; i < 5 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# SET : MIN = 0 , MAX = 100 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 11 # Randomization sucsessfull : Var = 8 # Randomization sucsessfull : Var = 36 # Randomization sucsessfull : Var = 64 # SET : MIN = 50 , MAX = 10 # Randomization failed # Randomization failed # Randomization failed # Randomization failed # Randomization failed