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

with_rng_version

RNG version


Description

Change the RNG version and restore it afterwards.

Usage

with_rng_version(version, code)

local_rng_version(version, .local_envir = parent.frame())

Arguments

version

[character(1)] an R version number, e.g. "3.5.0", to switch to the RNG this version of R uses. See RNGversion().

code

[any]
Code to execute in the temporary environment

.local_envir

The environment to apply the change to.

Details

with_rng_version() runs the code with the specified RNG version and resets it afterwards.

local_rng_version() changes the RNG version for the caller execution environment.

Value

[any]
The results of the evaluation of the code argument.

See Also

withr for examples

Examples

RNGkind()
with_rng_version("3.0.0", RNGkind())
with_rng_version("1.6.0", RNGkind())

with_rng_version("3.0.0",
  with_seed(42, sample(1:100, 3)))

with_rng_version("1.6.0",
  with_seed(42, sample(1:100, 3)))

RNGkind()

fun1 <- function() {
  local_rng_version("3.0.0")
  with_seed(42, sample(1:100, 3))
}

fun2 <- function() {
  local_rng_version("1.6.0")
  with_seed(42, sample(1:100, 3))
}

RNGkind()
fun1()
fun2()
RNGkind()

withr

Run Code 'With' Temporarily Modified Global State

v2.4.2
MIT + file LICENSE
Authors
Jim Hester [aut, cre], Kirill Müller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], Jennifer Bryan [ctb], Richard Cotton [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

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