[R] random numbers

Ross Boylan ross at biostat.ucsf.edu
Sat Jun 30 21:12:45 CEST 2007


Although this query was inspired by distributed random number
generation, one of the questions (#2 below) is a single-machine issue.

I call C++ code from R to generate simulated data.  I'm doing this on a
cluster, and use rmpi and rsprng.  While rsprng randomizes R-level
random numbers (e.g., from runif), it has no effect on the C code, which
is completely SPRNG and MPI ignorant.

Currently I generate a seed to pass into the C code, using
as.integer(runif(1, max=.Machine$integer.max)-.Machine$integer.max/2)
It seems to work.

Any comments on this approach?  Here are some issues I see:

1) The much simpler method of using the consecutive integers as seeds
also seemed to work.  This also has the advantage of repeatability.  I
avoided it because I was concerned it wouldn't be random enough.  Would
consecutive integers as in
  parLapply(cluster, seq(nSimulations), function(i) myfunction(seed=i))
be sufficient?

I suppose I could also generate all the random seeds on the master.

2) This got me thinking about how to generate random integers that span
the whole range of 32 bit signed integers.  The method show above only
spans half the range, since .Machine$integer.max = 2^31.  It also makes
some assumptions about the relation between the value in  .Machine
$integer.max and the seed for random numbers.  Interestingly,
integer.max was 2^31 despite running on a 64 bit powerpc, albeit under
the mostly 32 bit OS-X (I think Leopard--not the current one; Darwin
Kernel 7.9.0).

My understanding is that random number generators internally produce 32
bit integers, which then get converted into the desired distribution.
I'm a little surprised there doesn't seem to be a way to get at them.
Or is one supposed to do runif()*2^32-2^31?

3) Vagaries of the underlying C++ random number generator could also
complicate life.



More information about the R-help mailing list