Related to Randomization

Hi,

class packet;
  rand int a;
  constraint my_value{ a inside {[1:100]};}
endclass
module tb;
  initial begin
  packet pkt_1 , pkt_2;
    pkt_1=new();
    pkt_2=new();
  pkt_1.randomize();
  $display("values are=%0d",pkt_1.a);
  pkt_2.randomize();
    $display("values are=%0d",pkt_2.a);
  end
endmodule

In the above code, I am randomizing the class variable by two different handles. I am getting a different value, why?

Thanks in advance

In reply to Rajneesh Kumar:

What makes you think you should be getting the same values?

In reply to dave_59:

Thanks for reply…

Yes, I want the same values every time when I will randomize with any handle.

In reply to Rajneesh Kumar:

That would be a completely different question. The behavioral you want is not the same as the behavior the LRM requires.

Please read LRM section 18.14 Random stability to see what should happen, and read section 18.15 Manually seeding randomize to see how to get what you want to happen.

In reply to dave_59:

one way for doing this is, we can do shallow copy before randomization in the above example like-
pkt_2=new pkt_1;

now if I will randomize with either pkt_1 or pkt_2 it will give the same value.
correct me if I am wrong, please.

Hi Rajneesh,

one way for doing this is, we can do shallow copy before randomization in the above example like-
pkt_2=new pkt_1;

now if I will randomize with either pkt_1 or pkt_2 it will give the same value.
correct me if I am wrong, please.

No , you are not correct , whatever you are saying is like below example,

class packet;
rand int a;
constraint my_value{ a inside {[1:100]};}
endclass

module tb;
initial begin
packet pkt_1 , pkt_2;
pkt_1=new();
pkt_1.randomize();

$display("values of pkt_1 are=%0d",pkt_1.a);
pkt_2 = new pkt_1;
$display("values of pkt_2 are=%0d",pkt_2.a);

$display("------------------------------------");
pkt_1.randomize();
$display("values of pkt_1 are=%0d",pkt_1.a);
$display("values of pkt_2 are=%0d",pkt_2.a);
$display("------------------------------------");

end
endmodule

Result:-

Compiler version J-2014.12-SP1-1; Runtime version J-2014.12-SP1-1; Jun 10 10:38 2019
values of pkt_1 are=14
values of pkt_2 are=14

values of pkt_1 are=11
values of pkt_2 are=14

       V C S   S i m u l a t i o n   R e p o r t

Every time you have to do a shallow copy after randomization
But as Dave mentioned
Please read LRM section 18.14 Random stability to see what should happen, and read section 18.15 Manually seeding randomize to see how to get what you want to happen.

you can get the same value by manual seeding, like below example

class packet;
rand int a;
constraint my_value{ a inside {[1:100]};}

function new (integer seed);
//set a new seed for this instance
this.srandom(seed);
endfunction

endclass

module tb;

initial begin
packet pkt_1 , pkt_2;
pkt_1=new(3);
pkt_2=new(3);
// pkt_1.srandom(4); // re-seed
// pkt_2.srandom(4); // re-seed
pkt_1.randomize();
$display(“values are=%0d”,pkt_1.a);
pkt_2.randomize();
$display(“values are=%0d”,pkt_2.a);
end

endmodule

Result:-

Compiler version J-2014.12-SP1-1; Runtime version J-2014.12-SP1-1; Jun 10 10:47 2019
values are=18
values are=18
V C S S i m u l a t i o n R e p o r t

You can re-seed by passing new seed value in calling of srandom method like “pkt_1.srandom(4);” and it will overwrite the seed which we passed at the time of object creation i.e. pkt_1=new(3);