Randomization of multiple classes objects inside one class

Hi,

I have a class like this

class main_class;

   rand apr_ab apr_ab_obj;
   rand apr_ac apr_ac_obj;
   rand apr_ad apr_ad_obj;
   rand apr_ae apr_ae_obj;

endclass

each of apr_ab , apr_ac, apr_ad and apr_ae have about 100 interger variables that are rand.

Now if I randomize an object of main_class , is there any specific order in which the constraint solver solves apr_ab , apr_ac, apr_ad and apr_ae ? Or the solver solves all at once though there are no relationship between apr_ab , apr_ac, apr_ad and apr_ae.

Thanks

In reply to vbabusr:

The solver “solves” all random variables all together on the object being randomized, and recursively on all the child random class variables it contains. It doesn’t matter if there are constraint relationships between them or not.

To make its job easier, the solver will break the random variables up into sets with no constraint dependancies between the sets to make the smaller problems to solve.

In reply to dave_59:

Thank you Dave.

Also, If i have a code segment like below

class test;

  main_class m_obj;
  apr_ab obj_apr_ab;
  
  obj_apr_ab.randomize();

  m_obj.randomize() with { apr_ab_obj ==obj_apr_ab; }; <Question>


endclass

Can I use randomize with and pass a class as shown above ?

In reply to vbabusr:

Calling randomize never constructs a class object or modifies a class variable. Your constraint in invalid.

And it is not sufficient to just declare a class variable—you have to explicitly construct an object and assign its handle to a class variable. You cannot call m_obj.randomize(); until after you have constructed an object with m_obj = new();. And the 4 class objects you declared in main_class need to get constructed either by

class main_class;
   rand apr_ab apr_ab_obj;
   rand apr_ac apr_ac_obj;
   rand apr_ad apr_ad_obj;
   rand apr_ae apr_ae_obj;
   function new;
      apr_ab_obj = new;
      apr_ac_obj = new;
      apr_ad_obj = new;
      apr_ae_obj = new;
   endfunction
endclass

Or you can construct the class member after constructing the main_class

class test;
  main_class m_obj =new();
  apr_ab obj_apr_ab;
  function new;
    obj_apr_ab = new;
    m_obj.apr_ab_obj = obj_apr_ab; 
    m_obj.randomize(); 
  endfunction
endclass

It is difficult to recommend the best way to code this without knowing what you are trying to accomplish. https://xyproblem.info