Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_lecyer_cmrg_seed

Get a L'Ecuyer-CMRG seed either from an input seed or the current RNG state


Description

Get a L'Ecuyer-CMRG seed either from an input seed or the current RNG state

Usage

as_lecyer_cmrg_seed(seed)

is_lecyer_cmrg_seed(seed)

Arguments

seed

TRUE or NA, or a numeric vector of length one or seven.

Details

The as_lecyer_cmrg_seed() function preserves the current RNG state, that is, it leaves globalenv()$.Random.seed intact, which means it also leaved the RNG kind (RNGkind()) intact.

Per base::RNGkind(), a L'Ecuyer-CMRG seed comprise a length-seven integer vector of format .Random.seed <- c(rng.kind, n) where length(n) == 6L and rng.kind fulfills rng.kind %% 10000L == 407L.

Value

as_lecyer_cmrg_seed(seed) returns a L'Ecuyer-CMRG seed, which is a 7-digit integer vector, based on the input seed. If already a L'Ecuyer-CMRG seed, then seed is return as-is. If a scalar integer, then a random L'Ecuyer-CMRG seed is created based on this seed as the current RNG state. If seed = TRUE and the current seed is already a L'Ecuyer-CMRG seed, then then current seed (.Random.seed) is return as-is. If seed = TRUE and the current seed is not of the 'L'Ecuyer-CMRG' kind, or seed = NA, then a random one is created (based on the current RNG state). Any other values, including FALSE, is an error.

is_lecyer_cmrg_seed(seed) returns TRUE if seed is L'Ecuyer-CMRG seed, otherwise FALSE.

Examples

# The current RNG kind
okind <- RNGkind()
oseed <- globalenv()$.Random.seed

# (a) A L'Ecuyer-CMRG seed based on a numeric-scalar seed
seed1 <- future:::as_lecyer_cmrg_seed(42)
str(seed1)
## int [1:7] 10407 -2133391687 507561766 1260545903 1362917092 -1772566379 -1344458670
# The RNG kind and the RNG state is preserved
stopifnot(
  future:::is_lecyer_cmrg_seed(seed1),
  identical(RNGkind(), okind),
  identical(globalenv()$.Random.seed, oseed)
)

# (b) A L'Ecuyer-CMRG seed based on a L'Ecuyer-CMRG seed
seed2 <- future:::as_lecyer_cmrg_seed(seed1)
str(seed2)
## int [1:7] 10407 -2133391687 507561766 1260545903 1362917092 -1772566379 -1344458670
# The input L'Ecuyer-CMRG seed is returned as-is
stopifnot(identical(seed2, seed1))
# The RNG kind and the RNG state is preserved
stopifnot(
  future:::is_lecyer_cmrg_seed(seed2),
  identical(RNGkind(), okind),
  identical(globalenv()$.Random.seed, oseed)
)

# (c) A L'Ecuyer-CMRG seed based on the current RNG state
seed3 <- future:::as_lecyer_cmrg_seed(TRUE)
str(seed3)
## int [1:7] 10407 495333909 -1491719214 416071979 49340016 1956499377 899435966
stopifnot(future:::is_lecyer_cmrg_seed(seed3))


# All of the above calls preserve the RNG state including the RNG kind
stopifnot(
  identical(RNGkind(), okind),
  identical(globalenv()$.Random.seed, oseed)
)

future

Unified Parallel and Distributed Processing in R for Everyone

v1.21.0
LGPL (>= 2.1)
Authors
Henrik Bengtsson [aut, cre, cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.