The iRNGStream
creates a sequence of random number seeds
that are very "far apart" (2^127 steps) in the overall random
number sequence, so that each can be used to make a parallel,
psudo-independent random iterator. This uses
parallel::nextRNGStream and the "L'Ecuyer-CMRG" generator.
Value
An iteror which produces seed values.
vector to be passed to nextRNGStream
or nextRNGSubStream
.
An iteror which yields successive seed values.
Details
iRNGSubStream creates seeds that are somewhat less far apart (2^76 steps), which might be used as "substream" seeds.
Originally from the itertools
package.
References
For more details on the L'Ecuyer-CMRG generator, see
vignette("parallel", package="parallel")
.
Examples
global.seed <- .Random.seed
#> Error in eval(expr, envir, enclos): object '.Random.seed' not found
rng.seeds <- iRNGStream(313)
#> Error in set.seed(): argument "seed" is missing, with no default
print(nextOr(rng.seeds))
#> Error in eval(expr, envir, enclos): object 'rng.seeds' not found
print(nextOr(rng.seeds))
#> Error in eval(expr, envir, enclos): object 'rng.seeds' not found
# create three pseudo-independent and
# reproducible random number streams
it1 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
#> Error in eval(expr, envir, enclos): object 'rng.seeds' not found
it2 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
#> Error in eval(expr, envir, enclos): object 'rng.seeds' not found
it3 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))
#> Error in eval(expr, envir, enclos): object 'rng.seeds' not found
all(.Random.seed == global.seed)
#> Error in eval(expr, envir, enclos): object '.Random.seed' not found
take(it1, 5, "numeric") # 0 0 0 1 1
#> Error in eval(expr, envir, enclos): object 'it1' not found
take(it2, 5, "numeric") # 0 1 1 1 1
#> Error in eval(expr, envir, enclos): object 'it2' not found
take(it3, 5, "numeric") # 1 1 1 0 0
#> Error in eval(expr, envir, enclos): object 'it3' not found
# none of this affects the global seed
all(global.seed == .Random.seed)
#> Error in eval(expr, envir, enclos): object 'global.seed' not found
# \donttest{
# Compute random numbers in three parallel processes with three
# well-separated seeds. Requires package "foreach"
library(foreach)
foreach(1:3, rseed=iRNGSubStream(1970), .combine='c') %dopar% {
RNGkind("L'Ecuyer-CMRG") # would be better to initialize workers only once
assign('.Random.seed', rseed, pos=.GlobalEnv)
runif(1)
}
#> Warning: executing %dopar% sequentially: no parallel backend registered
#> Error in set.seed(): argument "seed" is missing, with no default
# }