Functions related to generating random seeds and utilising them for reproducibility.

gen_seed()

set_seed(seed)

fix_seed(reset = FALSE)

with_seed(seed, expression)

pull_seed(x)

## Arguments

seed The random seed to be used Should the fixed seed be forced to reset expression to be evaluated object to extract the seed from

## Value

gen_seed() returns a single numeric value

with_seed() returns the value of the evaluated expression after with the relevant seed as an attribute (if required)

pull_seed() returns a single numeric value

fix_seed() and set_seed() do not return anything

## Details

Random values are generated based on the current seed used by the R system. This means by deliberately setting a seed in R, we can make work reproducible.

## Functions

• gen_seed: Generates a random seed, which can be used in set_seed()

• set_seed: Sets the current seed

• fix_seed: Resets the seed to re-run code

• with_seed: Evaluates the expression after setting the seed. If seed is TRUE, then it first generates a seed using gen_seed(). Results are output with the seed attached (if set).#'

• pull_seed: Extracts the seed used to generate the results of with_seed()

## Examples

my_seed <- gen_seed()

set_seed(my_seed)
r_norm(n=10)
#>   -1.0571018 -1.5072919  0.1207109 -0.1105901 -0.7025600 -0.1372067
#>    1.1565282 -2.1143908  1.3294188  0.4709546set_seed(my_seed)
r_norm(n=10)
#>   -1.0571018 -1.5072919  0.1207109 -0.1105901 -0.7025600 -0.1372067
#>    1.1565282 -2.1143908  1.3294188  0.4709546

fix_seed()
r_norm(n=3)
#>  -1.1223793  0.2976979 -0.3536340
fix_seed()
r_norm(n=3)
#>  -1.1223793  0.2976979 -0.3536340
fix_seed(reset=TRUE)
r_norm(n=3)
#>  -0.8487979  0.7073074  0.8885189

res <- with_seed(my_seed, r_norm(n = 10))
res
#>   -1.0571018 -1.5072919  0.1207109 -0.1105901 -0.7025600 -0.1372067
#>    1.1565282 -2.1143908  1.3294188  0.4709546
#> attr(,"seed")
#>  1241451070

pull_seed(res)
#>  1241451070