Tutorials

RANDOMIZATION METHODS

Randomization Built-In Methods

openvera has randomize(),pre_randomize() and post_randomize() built-in function for randomization.
Calling randomize() causes new values to be selected for all of the random variables in an object.
To perform operations immediately before or after randomization,pre_randomize() and post_randomize() are used.

Randomize()

Every class has a virtual predefined function randomize() ,which is provided for generating a new value.Randomization function returns 1 if the solver finds a valid solution.We cannot override this predefined function.
It is strongly recomemded to check the return value of randomize function.Constraint solver never fails after one sucessful randomization,if solution space is not changed.For every randomization call,Check the return value ,solver may fail due to dynamically changing the constraints.
In the following example,there is no solution for Var < 100 and Var > 200,so the randomization failes.
The best way to check for randomization return value is by using assertion. assert(obj.randomize());

EXAMPLE

class Simple{
rand integer Var;
constraint c { Var < 100 ; Var > 200 ; }
}
program main{
Simple obj;
obj = new();
if(obj.randomize())
printf(" Randomization sucsessfull : Var = %0d ",obj.Var);
else
printf("Randomization failed");
}

RESULTS:

Constraint failure

Pre_randomize And Post_randomize

Every class contains pre_randomize() and post_randomize() methods, which are automatically called
by randomize() before and after computing new random values.When randomize() is called,it first invokes the pre_randomize,then randomize() then if the randomization is sucesusful,then only post_randomize is invoked.

EXAMPLE:
class simple{
printf(" PRE_RANDOMIZATION ");
}
printf(" PRE_RANDOMIZATION ");
}
}
program main{
simple obj = new();
void = obj.randomize();
}
RESULTS:

PRE_RANDOMIZATION PRE_RANDOMIZATION

 << PREVIOUS PAGE TOP NEXT PAGE >>