randomize() Vs 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.

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.

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.

Most important is that 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 the 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 rand_stimulus( int d_length );
int x, y, z, success;

success = std::randomize( x, y, z ) with { x < y ; x + y < d_length ; };
success = std::randomize( x, y ) with { y - x > d_length ; };

The task rand_stimulus above calls std::randomize twice resulting in two sets of random values for its local variables x, y, and z.

In the first call, variables x and y are constrained so that variable x is less than y and their sum is less than the task argument d_length, which is designated as a state variable.

In the second call, variables x and y are constrained so that their difference is greater than the state variable d_length.