Using rand_mode on a handle

Hi,

I would like to do as follows
Read a hierarchical string which is the name of a variable something like “top.agent.cfg.abc”
Assuming “abc” is and variable, i would like to set the rand_mode OFF or ON.
Essentially i want to get the string convert it to handle and use rand_mode on it
It looks like this is not possible in system verilog
I can get the handle to the variable (through vpi call) but there is no way to use rand_mode using that handle.
Can anyone suggest a method to do this?

Thanks

In reply to Pawamana:

Is this what you want ? (


class A;
  rand int a;
  
  constraint range_c {a inside {[1:10]};};
endclass
                      
module top;
  A a_i;
  string s = "abc"; //Get this from your DPI .
  
  initial begin
    a_i = new();
    if(s == "abc") a_i.a.rand_mode(0);
    if(s == "abc") a_i.range_c.constraint_mode(0);  //If you have a constraint on the variable, it's safer to disable that constraint too, else you may run into randomization failures.
    
    a_i.randomize();
    $display("a_i.a = %0d", a_i.a);
  end
endmodule