In reply to UVM_learner6:
It should work. I hope width of data is sufficient to hold the ab. For example, a,b are 32 bit then data should be at least 64bit(data=ab). This is just an example code. Please check variables width in your code.
class myPacket;
rand int a,b;
constraint c1 { a inside {[1:10]}; b inside {[1:10]}; }
function display ();
$display ("a : 0x%0h b : 0x%0h", a, b);
endfunction
endclass
class myPacket_1;
rand int a,b;
rand bit[63:0] data;
constraint c1 { data==a*b; }
function display ();
$display ("data : 0x%0h, a : 0x%0h b : 0x%0h", data, a, b);
endfunction
endclass
module tb_top;
myPacket pkt;
myPacket_1 pkt_1;
initial begin
pkt = new ();
pkt.randomize ();
pkt.display ();
pkt_1 = new();
pkt_1.randomize () with { a==pkt.a; b==pkt.b; };
pkt_1.display ();
end
endmodule