RE: [sv-ec] Question about seeding $urandom

From: Ryan, Ray <Ray_Ryan_at_.....>
Date: Wed Jun 10 2009 - 18:17:20 PDT
Hi, Cliff -

Yes the trick to using srandom is the built-in "process" class (Section
9.7)
and using the self() method to get the handle to the implicit class
instance
for the executing process.

Also related are the .get_randstate() and .set_randstate() methods. If
you
want to capture the current 'state' of a the RNG for a process (or
class)
you can use p.get_randstate(). This method return a string that encodes
the current state of the RNG. The string can then be used to reset the
state of the RNG. For example:
	process p;
	string rng_state;
      initial begin
	   ...
	   p = p.self();
	   rng_state = p.get_randstate();
   	   $display($urandom());
	   ...
	   p.set_randstate(rng_state);
	   $display($urandom());	// should be same # as above

Notes:
	1) The string returned by get_randstate is vendor specific. That
is, you
         can save the string (to a file) and then using it to initialize
the RNG state
         in a subsequent simulation. However, don't expect this to work
across
	   two different vendors.
	2) You also shouldn't manually edit the string. The string
passed to set_randstate
         should have been generated by get_randstate.
	3) There is no means of getting something like a 'seed value'
that corresponds
         to the current state of the RNG. One reason for this is that
the internal
         state of a RNG is generally more than 32-bits. So there isn't a
method
         that is the inverse of the srandom method.

Regards,
Ray

> -----Original Message-----
> From: owner-sv-ec@server.eda.org 
> [mailto:owner-sv-ec@server.eda.org] On Behalf Of Cliff Cummings
> Sent: Wednesday, June 10, 2009 5:43 PM
> To: sv-ec@server.eda.org
> Subject: RE: [sv-ec] Question about seeding $urandom
> 
> Hi, Ray -
> 
> This also helps! In fact, as I kept reading into section 
> 18.14.2, I found another working example of what I needed:
> 
> process::self.srandom(seed);
> 
> Your example (b) (below) is particularly interesting.
> 
> Essentially declare a process handle "p" and don't do 
> anything with it until we enter the initial block.
> 
> Once inside the initial block, grab a handle to the active 
> initial block p.self() (?? - is this analysis true) and 
> assign the initial block process handle to the declared "p" 
> handle and then proceed to seed the p-handle, which points to 
> the active initial block process (??)
> 
> I appreciate the education. I was not getting it from the LRM.
> 
> Regards - Cliff
> 
> At 05:24 PM 6/10/2009, Ryan, Ray wrote:
> >
> >Cliff,
> >
> >The $urandom and $urandom_range methods both use the 
> RNG(Random Number
> >Generator)
> >of the executing thread. Each thread has it's own RNG. The 
> state of the 
> >RNG for a dynamic process (thread) is initially seeded with the next 
> >random number from the RNG of the executing thread when the 
> a dynamic 
> >process is created. See
> >18.14.1 for
> >initialization of the RNG for static threads.
> >
> >The RNG state of a thread can be seeded by either:
> >a) calling $urandom(seed). The value of the parameter is 
> used to seed 
> >the internal
> >    RNG state of the executing thread. The method then 
> returns the next 
> >random
> >    number from initialized RNG. For example:
> >         $display( $urandom(10) );  // seed the thread RNG and print 
> >1st the random number
> >         $display( $urandom() );    // print the next (2nd) 
> random number
> >         $display( $urandom() );    // print the next (3rd) 
> random number
> >         $display( $urandom(1) );   // reset the RNG state 
> based on the
> >seed value 10
> >         $display( $urandom() );    // print next, should be same 2nd
> >value for seed 10
> >         $display( $urandom() );    // print next, should be same 3rd
> >value for seed 10
> >    In contrast $random(seed) does not have an internal state, it 
> >simply uses the seed
> >    parameter to compute a random number AND it also updates the 
> >parameter (which should
> >    be a variable).
> >    The updated seed variable can then be passed to $random(seed) to 
> >get the next random number.
> >
> >b) The RNG of a thread can also be seeded using the srandom 
> method the 
> >of the process (class).
> >    For example:
> >         process p;
> >         initial begin
> >                 p = p.self();   // get handle to the 
> 'process' class for
> >the executing thread
> >                 p.srandom(10);  // initialize the RNG of the thread 
> >with seed 10
> >                 $display($urandom());
> >                 $display($urandom());
> >                 $display($urandom());
> >    This should print the same sequence of 3 random number 
> as above in 
> >a).
> >
> >
> >- Ray Ryan
> >
> >
> >
> >
> > > -----Original Message-----
> > > From: owner-sv-ec@server.eda.org
> > > [mailto:owner-sv-ec@server.eda.org] On Behalf Of Cliff Cummings
> > > Sent: Wednesday, June 10, 2009 4:32 PM
> > > To: sv-ec@server.eda.org
> > > Subject: [sv-ec] Question about seeding $urandom
> > >
> > > Hi, All -
> > >
> > > I think random falls under EC, so at the bottom is the question.
> > >
> > > I was playing with $urandom and trying to seed it, and 
> have run into 
> > > problems related to my understanding of seeding this function.
> > >
> > > The following code is also in an attached file:
> > > urandomtest.sv (I run the nine different iterations using
> > > +define+RUN1 thru +define+RUN9)
> > >
> > > program urandomtest;
> > >    logic [7:0] a;
> > >    int         seed;
> > >
> > >    initial $monitor("a=%h", a);
> > >
> > >    initial begin
> > >      `ifdef RUN1
> > >      repeat(20) #1 a = $random();
> > >      `elsif RUN2
> > >      repeat(20) #1 a = $random(145);
> > >      `elsif RUN3
> > >      seed = 145;
> > >      repeat(20) #1 a = $random(seed);
> > >      `elsif RUN4
> > >      repeat(20) #1 a = $urandom();
> > >      `elsif RUN5
> > >      repeat(20) #1 a = $urandom(145);
> > >      `elsif RUN6
> > >      seed = 145;
> > >      repeat(20) #1 a = $urandom(seed);
> > >      `elsif RUN7
> > >      repeat(20) #1 a = $urandom_range(145);
> > >      `elsif RUN8
> > >      repeat(20) #1 a = $urandom_range(100,145);
> > >      `elsif RUN9
> > >      seed = 145;
> > >      repeat(20) #1 a = $urandom_range(seed);
> > >      `endif
> > >    end
> > > endprogram
> > >
> > > $random - normal Verilog behavior
> > > RUN1 - $random() generates 20 random numbers
> > >
> > > RUN2 - generates the same random number 20 times, each time using 
> > > the seed value of 145.
> > >
> > > RUN3 - generates 20 random numbers with the first number based on 
> > > the "seed" value of 145.
> > >
> > > $urandom - a few surprises
> > >
> > > RUN4 - generates 20 urandom numbers.
> > >
> > > RUN5 - generates the same urandom number 20 times, each 
> time using 
> > > the seed value of 145. In P1800-2009 Ballot Draft, clause 
> 18.13.1, 
> > > last sentence in the paragraph after the prototype function is a 
> > > little misleading when it says: "The random number 
> generator (RNG) 
> > > shall generate the same sequence of random numbers every time the 
> > > same seed is used."
> > >
> > > RUN6 - generates the same urandom number 20 times, each 
> time using 
> > > the seed value of 145 (seeding behavior is different than 
> Verilog's 
> > > $random from RUN3).
> > >
> > > $urandom_range - no big surprises
> > >
> > > RUN7 - generates 20 urandom_range numbers between 0-145
> > >
> > > RUN8 - generates 20 urandom_range numbers between 100-145
> > >
> > > RUN9 - generates 20 urandom_range numbers between 0-145
> > >
> > > Question - what is the correct way to seed the $urandom function 
> > > when used in procedural code outside of a class??
> > >
> > > Thanks for any feedback.
> > >
> > > Regards - Cliff
> > >
> > > ----------------------------------------------------
> > > Cliff Cummings - Sunburst Design, Inc.
> > > 14314 SW Allen Blvd., PMB 501, Beaverton, OR 97005
> > > Phone: 503-641-8446 / FAX: 503-641-8486 
> cliffc@sunburst-design.com / 
> > > www.sunburst-design.com Expert Verilog, SystemVerilog, 
> Synthesis and 
> > > Verification Training
> > >
> > > --
> > > This message has been scanned for viruses and dangerous 
> content by 
> > > MailScanner, and is believed to be clean.
> > >
> > >
> >
> >--
> >This message has been scanned for viruses and dangerous content by 
> >MailScanner, and is believed to be clean.
> 
> ----------------------------------------------------
> Cliff Cummings - Sunburst Design, Inc.
> 14314 SW Allen Blvd., PMB 501, Beaverton, OR 97005
> Phone: 503-641-8446 / FAX: 503-641-8486
> cliffc@sunburst-design.com / www.sunburst-design.com Expert 
> Verilog, SystemVerilog, Synthesis and Verification Training
> 
> 
> --
> This message has been scanned for viruses and dangerous 
> content by MailScanner, and is believed to be clean.
> 
> 

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Wed Jun 10 18:18:18 2009

This archive was generated by hypermail 2.1.8 : Wed Jun 10 2009 - 18:18:28 PDT