How should I randomize overridden class variable?

Hi,

In my_test i am overriding base_seq with my_seq. my_seq has random variables.
If i randomize base_seq in base_test, random variables in my_seq gets randomized and everything works fine.
But now i want to do randomize() with and i can not do that on base_seq.
My solution for this was


class my_class extends base_test;
   ... 
   my_seq m_my_seq;

  function void build_phase(uvm_phase phase);

    super.build_phase(phase);
    base_seq::type_id::set_type_override(my_seq::get_type());
    m_my_seq =my_seq::type_id::create("m_my_seq", this);  

  endfunction : build_phase

  task run_phase(uvm_phase phase);
    factory.print();
    phase.raise_objection(this);
      if(!m_my_seq.randomize() with {a==5;}) 
        `uvm_fatal(get_type_name(), "m_my_seq randomization failed!");
      $cast(m_base_seq,m_my_seq);
      super.run_phase(phase);
    phase.drop_objection(this);
  endtask : run_phase

endclass : my_test

class base_test;
   ...
   base_seq m_base_seq;
   ...
   m_base_seq =base_seq::type_id::create("m_base_seq", this); 
   ...
   m_base_seq.start(...);
   ...
endclass

class my_seq extends base_seq;
   rand int a;
   rand int b;
endclass

class base_seq;
endsclass


But in this case, it does not randomize.

Do you know how can i solve this?

Thanks in advance

In reply to EleneSajaia:

You didn’t show which phases you executed the code in base_test, but I assume you either wrote over my_base_seq with a new base_seq, or re-randomized my_base_seq. You show two calls to create() and I assume there are two calls to randomize(). Your override has no affect because you probably already called base_seq::type_id::create(). You should only be creating one sequence and randomizing it once.

The randomize with{} construct is not very OOP friendly. You can extent my_seq with the constraints you want for my_test

class my_seq_my_test extends my_seq
  `uvm_object_utils(my_seq_my_test)
  // only need constraints for my_test
  constraint c { a ==5; };
endclass
class my_class extends base_test;
   ... 
   my_seq m_my_seq;
 
  function void build_phase(uvm_phase phase);
    // override before calling super.build_phase
    base_seq::type_id::set_type_override(my_seq_my_test::get_type());
    super.build_phase(phase); 
  endfunction : build_phase
endclass : my_test