Skip to contents

This function calls sample and stores the values that are required to reproduce the exact same sampling in the attribute random_seed of the returned vector. When this attribute is passed to another call of this function, the values returned will be the same as in the first call.

Usage

reproducibleSample(..., FUN = sample, random_seed = NULL)

Arguments

...

arguments passed to FUN.

FUN

the sample function to be called. Default: sample.

random_seed

vector of integer as stored in .Random.seed.

Value

This function returns what sample returns with an attribute random_seed attached.

Examples

# Take a sample
x <- reproducibleSample(1:100, 10)
x
#>  [1] 58 63 62 34 42 50 37 19 84 78

# The full seed vector is returned in the attribute "random_seed"
random_seed <- attr(x, "random_seed")

# Take a new sample, this time passing the seed vector
y <- reproducibleSample(1:100, 10, random_seed = random_seed)
y
#>  [1] 58 63 62 34 42 50 37 19 84 78

# The values are identical to the values of the first sampling
identical(x, y)
#> [1] TRUE