[Rd] user supplied random number generators

Ross Boylan ross at biostat.ucsf.edu
Mon Aug 17 21:25:57 CEST 2009


On Sun, 2009-08-16 at 21:24 +0200, Petr Savicky wrote:
> Dear Ross Boylan:
> 
> Some time ago, you sent an email to R-devel with the following.
> > I got into this because I'm trying to extend the rsprng code; sprng
> > returns its state as a vector of bytes.  Converting these to a vector of
> > integers depends on the integer length, hence my interest in the exact
> > definiton of integer.  I'm interested in lifetime because I believe
> > those bytes are associated with the stream and become invalid when the
> > stream is freed; furthermore, I probably need to copy them into a buffer
> > that is padded to full wordlength.  This means I allocate the buffer
> > whose address is returned to the core R RNG machinery.  Eventually
> > somebody needs to free the memory.
> > 
> > Far more of my rsprng adventures are on
> > http://wiki.r-project.org/rwiki/doku.php?id=packages:cran:rsprng.  Feel
> > free to read, correct, or extend it.
> 
> I am interested to know, what is the current state of your project.
I did figure out some of the lifetime issues; SPRNG does allocate memory
when you ask it for its state.  I also realized that for several reasons
it would not be appropriate to hand that buffer to R.

I've reworked the page extensively since it had the section you quote.
See particularly the "Getting and Setting Stream State" section near the
bottom.

I submitted patches to hook rsprng into R's standard machinery for
stream state (the user visible part of which is .Random.seed).  The
package developer has reservations about applying them.

As a practical matter, I shifted my package's C code to call back to R
to get random numbers.  If rsprng is loaded and activated, my code will
use it.  I also eliminated all attempts to set the seed in my code.  For
rsprng, in its current form, the R set.seed() function is a no-op and
you have to use an rsprng function to set the seed (generally when
activating the library).
> 
> There is a package rngwell19937 with a random number generator, which i develop
> and use for several parallel processes. Setting a seed may be done by a vector,
> one of whose components is the process number. The initialization then provides
> unrelated sequences for different processes.
That sounds interesting; thanks for pointing it out.
> 
> Seeding by a vector is also available in the initialization of Mersenne Twister
> from 2002. See mt19937ar.c (ar for array) at
>   http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
> Unfortunately, seeding by a vector is not available in R base. R uses
> Mersenne Twister, but with an initialization by a single number.
I think that one could write to .Random.seed directly to set a vector
for many of the generators.  ?.Random.seed does not recommend this and
notes various limits and hazards of this strategy.

Ross



More information about the R-devel mailing list