Tuesday, 17 March 2015

Difference between randomize() and std::randomize()

The built-in class randomize method operates exclusively on class member variables. Using classes to model the data to be randomized is a powerful mechanism that enables the creation of generic, reusable objects containing random variables and constraints that can be later extended, inherited, constrained, overridden, enabled, disabled, and merged with or separated from other objects. The ease with which classes and their associated random variables and constraints can be manipulated makes classes an ideal vehicle for describing and manipulating random data and constraints.

Variables in an object are randomized using the randomize() class method.
Every class has a built-in randomize() virtual method, declared as follows: virtual function int randomize();
The randomize() method is a virtual function that generates random values for all the active random
variables in the object, subject to the active constraints.

However, some less-demanding problems that do not require the full flexibility of classes can use a simpler mechanism to randomize data that do not belong to a class. The scope randomize function, std::randomize(), enables users to randomize data in the current scope without the need to define a class or instantiate a class object.

The std::randomize function behaves exactly the same as a class randomize method, except that it operates on the variables of the current scope instead of class member variables.

Adding constraints with std::randomize() with

The std::randomize() with form of the scope randomize function allows users to specify random
constraints to be applied to the local scope variables.
When specifying constraints, the arguments to the scope randomize function become random variables; all other variables are considered state variables.

task stimulus( int length );
int a, b, c, success;
success = std::randomize( a, b, c ) with { a < b ; a + b < length ; };
...
success = std::randomize( a, b ) with { b - a > length ; };
...
endtask

The task stimulus above calls std::randomize twice resulting in two sets of random values for its local
variables a, b, and c.
In the first call, variables a and b are constrained so that variable a is less than b and their sum is less than the task argument length, which is designated as a state variable.
In the second call, variables a and b are constrained so that their difference is greater than the state variable length.

No comments:

Post a Comment