Learn R Programming

DHARMa (version 0.3.3.0)

getRandomState: Record and restore a random state

Description

The aim of this function is to record, manipualate and restor a random state

Usage

getRandomState(seed = NULL)

Arguments

seed

seed argument to set.seed(). NULL = no seed, but random state will be restored. F = random state will not be restored

Value

a list with various infos about the random state that after function execution, as well as a function to restore the previous state before the function execution

Details

This function is intended for two (not mutually exclusive tasks)

a) record the current random state

b) change the current random state in a way that the previous state can be restored

Examples

Run this code
# NOT RUN {
# testing the function in standard settings

set.seed(13)
runif(1)
x = getRandomState(123)
runif(1)
x$restoreCurrent()
runif(1)

# values outside set /restore are identical to

set.seed(13)
runif(2)

# if no seed is set, this will also be restored

rm(.Random.seed) # now, there is no random seed
x = getRandomState(123)
exists(".Random.seed")  # TRUE
runif(1)
x$restoreCurrent()
exists(".Random.seed") # False

# with seed = false 

x = getRandomState(seed = FALSE)
exists(".Random.seed")
runif(1)
x$restoreCurrent()
exists(".Random.seed")
# }

Run the code above in your browser using DataLab