inst/testme/test-withDoRNG.R

#' @tags withDoRNG
#' @tags %dopar%
#' @tags %dorng%
#' @tags rng doRNG
#' @tags sequential multisession cluster multicore

library(doFuture)

strategies <- future:::supportedStrategies()

if (require("doRNG")) {
  truth <- NULL
  
  my_fcn_dopar <- function() {
    y <- foreach(i = 1:10) %dopar% {
      sample.int(n = 100L, size = 1L)
    }
    mean(unlist(y))
  }

  my_fcn_dorng <- function() {
    y <- foreach(i = 1:10) %dorng% {
      sample.int(n = 100L, size = 1L)
    }
    mean(unlist(y))
  }

  registerDoFuture()

  message("*** withDoRNG() ...")
  print(sessionInfo())

  for (strategy in strategies) {
    message(sprintf("- plan('%s') ...", strategy))
    plan(strategy)

    if (is.null(truth)) {
      set.seed(1234)
      truth <- my_fcn_dorng()
    }
    
    set.seed(1234)
    res <- withDoRNG(my_fcn_dopar())
    print(res)
    stopifnot(identical(res, truth))

    # Shutdown current plan
    plan(sequential)

    message(sprintf("- plan('%s') ... DONE", strategy))
  } ## for (strategy ...)

  message("*** withDoRNG() ... DONE")

} ## if (require("doRNG"))

Try the doFuture package in your browser

Any scripts or data that you put into this service are public.

doFuture documentation built on June 8, 2025, 2:04 p.m.