Randomization

During a generic randomization, how do we get to know the seed value?
And is it possible to view the generated random values without display statement?

In reply to Bharathy :

In SystemVerilog the initial seed is a 32-bit integer that gets converted to a much larger internal random number generator (RNG) that you can get with get_randstate().

Like any variable, wither it be random or not, you need some file I/O command to view the variable. Or your tool may let you dump values to a database that can be viewed in a GUI.

In reply to dave_59:

In reply to Bharathy :
In SystemVerilog the initial seed is a 32-bit integer that gets converted to a much larger internal random number generator (RNG) that you can get with get_randstate().

I am not quite clear with this statement.

Say, for example consider this code

module temp; 
  class rand_class;
    rand int unsigned data;
  endclass  
  rand_class obj;   
    initial
    begin      
      obj = new();
      repeat(10) begin
        obj.randomize();
        $display("Data = %d, seed = %d",obj.data,obj.get_randstate());  end    
    end  
endmodule

Here, I am using a single thread obj.randomize() which I am calling 10 times. Only one RNG corresponding to that thread will be activated and generates random numbers.
So, the randstate should be same for all 10 repetitions right?

But the results are showing different randstates.

In reply to Bharathy :

You are $displaying the object RNG, not the thread RNG. The thread RNG seeds the object RNG upon construction of the object and advances the thread RNG. Each call to randomize() advances the object RNG and leaves the thread RNG alone.

In reply to dave_59:

Okay…

I just modified the same code and I am inserting a seed manually.

module temp; 
  class rand_class;
    rand int unsigned data;   
    function new(int seed);
      srandom(seed);
    endfunction
  endclass  
  
  rand_class obj;   
    initial
    begin      
      obj = new(10);
      $display("SEED = %d",obj.get_randstate());
      repeat(5) begin
        obj.randomize();
        $display("Data = %d, seed = %d",obj.data,obj.get_randstate());  end    
    end  
endmodule

So, here on which RNG will the seed 10 be loaded? If it is for object thread, then through first $display for obj.get_randstate() I should be getting 10.

Here is the output,

SEED =  4627009825161508818790549032975409576912747608007613670663354967688005127426567537619378540336574321059771681060756351887347663700834624509726131308091994
Data = 1259199836, seed =  2532006271069517679567699893160377841590441643497274272351457228610612951981757327074417830179063467247960281449516660673757496792152083733712046514526810
Data = 1918125812, seed =  4626976265854281180514947197437822078477535798474982797456824333358331421334425210844417182152310342353194914306311223319518503703649396910909870961220184
Data = 2263373791, seed =  2576402305486693448314360481394217134024106353880668131339340534213604682159408462868979423471588136234311296067687617247286999169849357467176591408257114
Data = 1160894215, seed =  4627009824649339133011204972591200958016150187620687913580408860257674102029464678299947477957044723251536943865625013119475372925999534663865015276690008
Data = 2156368458, seed =  4618826223773684211888993272211760420365008116347779968445576592813163782487979542097848440530407180683387174999129698488042551378273532800632930399050330

I am unable to figure out where my seed 10 is initiating…

In reply to Bharathy :

the initial seed is a 32-bit integer that gets converted to a much larger internal random number generator (RNG) that you can get with get_randstate().

In reply to dave_59:

I get it now.
Thanks for the quick response Dave :)

One more question,
The difference between $srandom() and set_randstate() is that srandom can be used in all coding blocks(meaning with/without classes) whereas set_randstate is applicable only with respect to class->objects.
So, $srandom has more advantage over set_randstate. Is this correct?

In reply to Bharathy :

I think you meant the srandom() method, there is no $srandom() function, Both srandom() and set_randstate() can be called on threads or objects. The built-in process class is just another object.

The only place to use set_randstate() is with a value string returned by an earlier get_randstate(). You do this when you need to preserve the RNG from being disturbed by some other operation, like something used for debugging.

...
state = get_randstate();
do_something_else; // want to guarantee that calling this does not disturb the RNG
set_randstate(state);
...

The srandom() method is used when you don’t want to use SystemVerilog’s built-in seeding mechanism

In reply to dave_59:

Thanks for the clarity Dave :)

But if I need to know the seed for each and every variable I am randomizing, is that possible to obtain? I tried it and is throwing error.

I mean, consider this code
module test;

class sample;
rand int unsigned a,b;
endclass

sample obj1,obj2;
initial begin
obj1 = new();
$display(“Initial seed = %x”,obj1.get_randstate());
repeat(5) begin
obj1.randomize();
$display(“a=%d, b=%d, seed=%x”,obj1.a,obj1.b,obj1.get_randstate()); end
end

endmodule

How can I get the randstate for a & b separately?

In reply to Bharathy :

Only objects have a randstate, not individual variables.

And I don’t think you are getting the relationship between the seed and the randstate

In reply to dave_59:

Seed is given to activate an RNG and to specify the initial point, which can be passed manually too.
When you are passing a seed to an RNG, it gets converted into a larger number(dependent on RNG) which undergoes algorithm to generate the required random number. And this larger number is what we get through randstate.

This is my understanding. Is this correct?