From nilsson.henric at gmail.com  Thu Mar  6 11:17:36 2014
From: nilsson.henric at gmail.com (Henric Winell)
Date: Thu, 06 Mar 2014 11:17:36 +0100
Subject: [Rd] 'parallel' package changes '.Random.seed'
Message-ID: <53184B40.2050604@gmail.com>

Hi,

I've implemented parallelization in one of my packages using the 
'parallel' package -- many thanks for providing it!

In my package I'm importing 'parallel' and so added it to the 
DESCRIPTION file's 'Import:' tag and also added a 
'importFrom("parallel", ...)' statement in the NAMESPACE file.

Parallelization works nicely, but my package no longer passes any parts 
of its (unparallelized) checks that depends on random number generation, 
e.g., the simulated data in the check suite are no longer the same as 
before parallelization was added.  This seems to be due to 'parallel' 
changing '.Random.seed' when loading its name space:

 > set.seed(1)
 > rs1 <- .Random.seed
 > rnorm(1)
[1] -0.6264538
 > set.seed(1)
 > rs2 <- .Random.seed
 > identical(rs1, rs2)
[1] TRUE
 > loadNamespace("parallel")
<environment: namespace:parallel>
 > rs3 <- .Random.seed
 > identical(rs1, rs3)
[1] FALSE
 > rnorm(1)
[1] -0.3262334
 > set.seed(1)
 > rs4 <- .Random.seed
 > identical(rs1, rs4)
[1] TRUE

I've taken a look at the 'parallel' source code, and in a few places a 
call to 'runif(1)' is issued.  So, what effectively seems to happen when 
'parallel' is loaded is

 > set.seed(1)
 > runif(1)
[1] 0.2655087
 > rnorm(1)
[1] -0.3262334

which reproduces the above.  But is this really necessary?  And more 
importantly (at least to me):  Can it somehow be avoided?

The current state of affairs is a bit unfortunate, since it implies that 
a user just by loading the new parallelized version of my package can no 
longer reproduce any subsequent results depending on random number 
generation (unless a call to 'set.seed' was issued *after* attaching my 
package).

I'd be most grateful for any help that you're able to provide here. 
Many thanks!

Kind regards,
Henric Winell


> sessionInfo()
R Under development (unstable) (2014-01-26 r64897)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=sv_SE.UTF-8        LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_3.1.0 parallel_3.1.0 tools_3.1.0



[Rd] 'parallel' package changes '.Random.seed'
Henric Winell