www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - random getstate/setstate

reply telengard <bsturk comcast.net> writes:
I'm not sure if this is the right group for "newbie" type questions if not I
will repost.

I'm considering porting my C++ game to D (had been python, AI was way too slow,
now memory management is driving me nuts).  So far just about everything on my
long checklist is there, even some stuff that will make it much easier/cleaner
like the object::factory() stuff.

The one thing I haven't been able to find is support for getstate/setstate for
tucking away the state of the RNG.  I need this to support replays.  I've
searched and have not been able to find it.  Would it be too difficult to
somehow get at it through the C interfacing capabilities?

thanks for any info,
~telengard
Aug 10 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
import
   std.c.time,
   std.random,
   std.stdio;


class Random
{
   private uint seed, index;

   this()
   {
      seed = time( null );
      rand_seed( seed, index );
   }

   this( uint _seed, uint _index = 0 )
   {
     seed = _seed;
     index = _index;
     rand_seed( seed, index );
   }

   void reseed( )
   {
     seed = time( null );
     index = 0;
     rand_seed( seed, index );
   }

   void reseed( uint _seed, uint _index = 0 )
   {
     seed = _seed;
     index = _index;
     rand_seed( seed, index );
   }

   synchronized int next() { ++index; return rand(); }

   uint getseed() { return seed; }
   uint getindex() { return index; }
}

void main()
{
   //first random
   auto r = new Random;
   writefln("%d %d %d", r.next, r.next, r.next);
   //reset seed..
   auto r2 = new Random(r.getseed);
   writefln("%d %d %d", r2.next, r2.next, r2.next);

   //get next 3
   writefln("%d %d %d", r.next, r.next, r.next);

   //use index to get same numbers with seed
   r.reseed(r.getseed, r.getindex-3);
   writefln("%d %d %d", r.next, r.next, r.next);
}
Aug 10 2008
parent telengard <bsturk comcast.net> writes:
Thank you very much Wyverex for the help.

If I'm reading the code correctly the index is doing all the magic to keep
track of where to resume?

thanks,
~telengard

 import
    std.c.time,
    std.random,
    std.stdio;
 
 
 class Random
 {
    private uint seed, index;
 
    this()
    {
       seed = time( null );
       rand_seed( seed, index );
    }
 
    this( uint _seed, uint _index = 0 )
    {
      seed = _seed;
      index = _index;
      rand_seed( seed, index );
    }
 
    void reseed( )
    {
      seed = time( null );
      index = 0;
      rand_seed( seed, index );
    }
 
    void reseed( uint _seed, uint _index = 0 )
    {
      seed = _seed;
      index = _index;
      rand_seed( seed, index );
    }
 
    synchronized int next() { ++index; return rand(); }
 
    uint getseed() { return seed; }
    uint getindex() { return index; }
 }
 
 void main()
 {
    //first random
    auto r = new Random;
    writefln("%d %d %d", r.next, r.next, r.next);
    //reset seed..
    auto r2 = new Random(r.getseed);
    writefln("%d %d %d", r2.next, r2.next, r2.next);
 
    //get next 3
    writefln("%d %d %d", r.next, r.next, r.next);
 
    //use index to get same numbers with seed
    r.reseed(r.getseed, r.getindex-3);
    writefln("%d %d %d", r.next, r.next, r.next);
 }
 

Aug 10 2008