Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Non-reproducible RNG when using .options.RNG and %dorng% #12

Closed
HenrikBengtsson opened this issue Oct 22, 2019 · 3 comments
Closed

Comments

@HenrikBengtsson
Copy link

First call to foreach(..., .options.RNG=1234) %dorng% { ... } uses a different RNG state.

Minimal reproducible example

$ R --quiet --vanilla

library(doRNG)
registerDoSEQ()

# single %dorng% loops are reproducible
r1 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
r2 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
r3 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
stopifnot(identical(r3, r2)) # OK
stopifnot(identical(r1, r2)) # FAILS
> str(list(r1=r1, r2=r2))
List of 2
 $ r1:List of 4
  ..$ : num 0.479
  ..$ : num 0.922
  ..$ : num 0.0592
  ..$ : num 0.191
  ..- attr(*, "rng")=List of 4
  .. ..$ : int [1:7] 10407 -305383241 69318028 -1161659107 -348804806 -1127582349 -343284712
  .. ..$ : int [1:7] 10407 1222430165 -1386318120 1144759979 748500427 690571908 329037778
  .. ..$ : int [1:7] 10407 215137210 -1421031558 1452323561 -2060097535 712279522 -278425444
  .. ..$ : int [1:7] 10407 -1017432992 1583835527 1997665660 821136125 1282044776 1045008570
 $ r2:List of 4
  ..$ : num 0.473
  ..$ : num 0.413
  ..$ : num 0.622
  ..$ : num 0.349
  ..- attr(*, "rng")=List of 4
  .. ..$ : int [1:7] 10407 -305777535 -1394370482 -1723143049 2071488076 1659356893 -1081051142
  .. ..$ : int [1:7] 10407 1325398954 -1161873690 -494229462 -159563550 560770250 333672325
  .. ..$ : int [1:7] 10407 297685860 239054748 -160895377 224208364 -348437793 -1537437949
  .. ..$ : int [1:7] 10407 -1436965524 -782679284 2145224753 2079003111 -745179495 -788808701

Troubleshooting

Note how it's the first call with %dorng% that gives a different answer. Because of this, I strongly suspect that the RNG state is updated after .options.RNG has been applied but before the loop expression is evaluated.

Session info

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

Random number generation:
 RNG:     L'Ecuyer-CMRG 
 Normal:  Inversion 
 Sample:  Rejection 
 
locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.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     

other attached packages:
[1] doRNG_1.7.1    rngtools_1.4   pkgmaker_0.27  registry_0.5-1 foreach_1.4.7 

loaded via a namespace (and not attached):
 [1] compiler_3.6.1   magrittr_1.5     parallel_3.6.1   tools_3.6.1     
 [5] withr_2.1.2      stringi_1.4.3    codetools_0.2-16 iterators_1.0.12
 [9] stringr_1.4.0    digest_0.6.21    xtable_1.8-4     bibtex_0.4.2
@HenrikBengtsson
Copy link
Author

Another clue: Initiating the RNG state before calling the first foreach() will make the results reproducible;

library(doRNG)
registerDoSEQ()
sample.int(1L)
r1 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
r2 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
r3 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
stopifnot(identical(r3, r2)) # OK
stopifnot(identical(r1, r2)) # OK

@renozao
Copy link
Owner

renozao commented Nov 12, 2019

@HenrikBengtsson did you get a chance to test this fix?

@HenrikBengtsson
Copy link
Author

Thxs. I can confirm that the master branch solves the problem (tested with my original example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants