Generate Sequence of Random Streams

Description

Create a given number of seeds for L'Ecuyer's RNG, that can be used to seed parallel computation, making them fully reproducible.

RNGseq_seed generates the -- next -- random seed used as the first seed in the sequence generated by RNGseq.

Usage

RNGseq(n, seed = NULL, ..., simplify = TRUE, version = 2)

RNGseq_seed(seed = NULL, normal.kind = NULL, verbose = FALSE, version = 2)

Arguments

n
Number of streams to be created
seed
seed specification used to initialise the set of streams using RNGseq_seed.
simplify
a logical that specifies if sequences of length 1 should be unlisted and returned as a single vector.
...
extra arguments passed to RNGseq_seed.
normal.kind
Type of Normal random generator. See RNG.
verbose
logical to toggle verbose messages
version
version of the function to use, to reproduce old behaviours. Version 1 had a bug which made the generated stream sequences share most of their seeds (!), as well as being not equivalent to calling set.seed(seed); RNGseq_seed(NULL). Version 2 fixes this bug.

Value

a list of integer vectors (or a single integer vector if n=1 and unlist=TRUE).

a 7-length numeric vector.

Details

This ensures complete reproducibility of the set of run. The streams are created using L'Ecuyer's RNG, implemented in R core since version 2.14.0 under the name "L'Ecuyer-CMRG" (see RNG).

Generating a sequence without specifying a seed uses a single draw of the current RNG. The generation of a sequence using seed (a single or 6-length numeric) a should not affect the current RNG state.

Examples


RNGseq(3)
## [[1]]
## [1]        407 -978439420 1478102581 -305458830 -856571317  650208528
## [7] 1074773457
## 
## [[2]]
## [1]         407  1609044590  1253481101  1620516322  -928645980 -2106955413
## [7]  1296706785
## 
## [[3]]
## [1]         407  -955365930  1376808480 -1027324033  1373558928   821426560
## [7] -1902732257
RNGseq(3)
## [[1]]
## [1]         407  1988955604   645650117  -237503294 -1634909861  1690957024
## [7]  -334989471
## 
## [[2]]
## [1]         407 -2020884048 -1713953665  2113742629   159255880 -1097925173
## [7]  2099880136
## 
## [[3]]
## [1]         407   594549532  1801224098   798612488 -1899955208 -1353578868
## [7]  1074518195
RNGseq(3, seed=123)
## [[1]]
## [1]         407   642048078    81368183 -2093158836   506506973  1421492218
## [7] -1906381517
## 
## [[2]]
## [1]         407  1340772676 -1389246211  -999053355  -953732024  1888105061
## [7]  2010658538
## 
## [[3]]
## [1]         407 -1318496690  -948316584   683309249  -990823268 -1895972179
## [7]  1275914972
# or identically
set.seed(123)
identical(RNGseq(3), RNGseq(3, seed=123))
## [1] TRUE
## Don't show: 
set.seed(123)
stopifnot( identical(RNGseq(3), RNGseq(3, seed=123)) )
## End Don't show

RNGseq(3, seed=1:6, verbose=TRUE)
## # Original RNG: Mersenne-Twister - Inversion [403, 1, 515190382, 2133433928, 917665867, 1283494313, 1101294840, ...]
## # Directly use 6-long seed: 1, 2, 3, 4, 5, 6 ... OK
## # Seed RNGkind is: L'Ecuyer-CMRG - Inversion [407, 1, 2, 3, 4, 5, 6]
## # Restoring RNG: Mersenne-Twister - Inversion [403, 1, 515190382, 2133433928, 917665867, 1283494313, 1101294840, ...]
## [[1]]
## [1] 407   1   2   3   4   5   6
## 
## [[2]]
## [1]         407  -447371532   542750874  -935969228  -269326340   701604884
## [7] -1748056907
## 
## [[3]]
## [1]         407   311773008 -1393648596   433058656  -545474683  2059732357
## [7]   994549473
# select Normal kind
RNGseq(3, seed=123, normal.kind="Ahrens")
## [[1]]
## [1]         107   642048078    81368183 -2093158836   506506973  1421492218
## [7] -1906381517
## 
## [[2]]
## [1]         107  1340772676 -1389246211  -999053355  -953732024  1888105061
## [7]  2010658538
## 
## [[3]]
## [1]         107 -1318496690  -948316584   683309249  -990823268 -1895972179
## [7]  1275914972
## generate a seed for RNGseq
# random
RNGseq_seed()
## [1]        407 -770194214  920761491  494670008 -102057127 -942372026
## [7] 1535907087
RNGseq_seed()
## [1]         407    82000378 -1377755341  -888237352  -352809223  1464214118
## [7] -1790002769
RNGseq_seed(NULL)
## [1]         407  1285013706  -887512125 -1801713112  -140498423 -1754456522
## [7]  -350228673
# fixed
RNGseq_seed(1)
## [1]         407 -1535484873  1222746892  1963142301   158053050 -1240755981
## [7]  -292394600
RNGseq_seed(1:6)
## [1] 407   1   2   3   4   5   6

# `RNGseq_seed(1)` is identical to
set.seed(1)
s <- RNGseq_seed()
identical(s, RNGseq_seed(1))
## [1] TRUE
## Don't show: 
 stopifnot(identical(s, RNGseq_seed(1))) 
## End Don't show

See also

RNGseq