| 1 |
#' Random seed |
| 2 |
#' |
| 3 |
#' `with_seed()` runs code with a specific random seed and resets it afterwards. |
| 4 |
#' |
| 5 |
#' @template with |
| 6 |
#' @param seed `[integer(1)]`\cr The random seed to use to evaluate the code. |
| 7 |
#' @param .local_envir `[environment]`\cr The environment to use for scoping. |
| 8 |
#' @param .rng_kind,.rng_normal_kind,.rng_sample_kind |
| 9 |
#' `[character(1)]`\cr Kind of RNG to use. Passed as the `kind`, |
| 10 |
#' `normal.kind`, and `sample.kind` arguments of [RNGkind()]. |
| 11 |
#' @examples |
| 12 |
#' # Same random values: |
| 13 |
#' with_preserve_seed(runif(5)) |
| 14 |
#' with_preserve_seed(runif(5)) |
| 15 |
#' |
| 16 |
#' # Use a pseudorandom value as seed to advance the RNG and pick a different |
| 17 |
#' # value for the next call: |
| 18 |
#' with_seed(seed <- sample.int(.Machine$integer.max, 1L), runif(5)) |
| 19 |
#' with_seed(seed, runif(5)) |
| 20 |
#' with_seed(seed <- sample.int(.Machine$integer.max, 1L), runif(5)) |
| 21 |
#' @export |
| 22 |
with_seed <- function( |
| 23 |
seed, |
| 24 |
code, |
| 25 |
.rng_kind = NULL, |
| 26 |
.rng_normal_kind = NULL, |
| 27 |
.rng_sample_kind = NULL |
| 28 |
) { |
| 29 |
force(seed) |
| 30 |
rng_kind <- list(.rng_kind, .rng_normal_kind, .rng_sample_kind) |
| 31 |
|
| 32 |
with_preserve_seed({ |
| 33 |
set_seed(list(seed = seed, rng_kind = rng_kind)) |
| 34 |
code |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
#' @rdname with_seed |
| 39 |
#' @export |
| 40 |
local_seed <- function( |
| 41 |
seed, |
| 42 |
.local_envir = parent.frame(), |
| 43 |
.rng_kind = NULL, |
| 44 |
.rng_normal_kind = NULL, |
| 45 |
.rng_sample_kind = NULL |
| 46 |
) { |
| 47 |
old_seed <- get_seed() |
| 48 |
|
| 49 |
defer(envir = .local_envir, { |
| 50 |
if (is.null(old_seed)) { |
| 51 |
rm_seed() |
| 52 |
} else { |
| 53 |
set_seed(old_seed) |
| 54 |
} |
| 55 |
}) |
| 56 |
|
| 57 |
rng_kind <- list(.rng_kind, .rng_normal_kind, .rng_sample_kind) |
| 58 |
set_seed(list(seed = seed, rng_kind = rng_kind)) |
| 59 |
|
| 60 |
# FIXME |
| 61 |
invisible(seed) |
| 62 |
} |
| 63 |
|
| 64 |
#' @rdname with_seed |
| 65 |
#' @description |
| 66 |
#' `with_preserve_seed()` runs code with the current random seed and resets it |
| 67 |
#' afterwards. |
| 68 |
#' |
| 69 |
#' @export |
| 70 |
with_preserve_seed <- function(code) { |
| 71 |
old_seed <- get_seed() |
| 72 |
if (is.null(old_seed)) { |
| 73 |
on.exit(rm_seed(), add = TRUE) |
| 74 |
} else { |
| 75 |
on.exit(set_seed(old_seed), add = TRUE) |
| 76 |
} |
| 77 |
|
| 78 |
code |
| 79 |
} |
| 80 |
|
| 81 |
#' @rdname with_seed |
| 82 |
#' @export |
| 83 |
local_preserve_seed <- function(.local_envir = parent.frame()) { |
| 84 |
old_seed <- get_seed() |
| 85 |
|
| 86 |
defer( |
| 87 |
if (is.null(old_seed)) { |
| 88 |
rm_seed() |
| 89 |
} else { |
| 90 |
set_seed(old_seed) |
| 91 |
}, |
| 92 |
envir = .local_envir |
| 93 |
) |
| 94 |
|
| 95 |
invisible(old_seed) |
| 96 |
} |
| 97 |
|
| 98 |
has_seed <- function() { |
| 99 |
exists(".Random.seed", globalenv(), mode = "integer", inherits = FALSE) |
| 100 |
} |
| 101 |
|
| 102 |
get_seed <- function() { |
| 103 |
# Return `NULL` (rather than a list with `random_seed = NULL`) when no |
| 104 |
# seed exists. Callers branch on `is.null()` to decide whether to remove |
| 105 |
# `.Random.seed` on exit instead of assigning `NULL` to it. |
| 106 |
if (!has_seed()) { |
| 107 |
return(NULL) |
| 108 |
} |
| 109 |
list( |
| 110 |
random_seed = get0( |
| 111 |
".Random.seed", |
| 112 |
globalenv(), |
| 113 |
mode = "integer", |
| 114 |
inherits = FALSE |
| 115 |
), |
| 116 |
rng_kind = RNGkind() |
| 117 |
) |
| 118 |
} |
| 119 |
|
| 120 |
set_seed <- function(seed) { |
| 121 |
restore_rng_kind(seed$rng_kind) |
| 122 |
|
| 123 |
if (is.null(seed$seed)) { |
| 124 |
assign(".Random.seed", seed$random_seed, globalenv()) |
| 125 |
} else { |
| 126 |
set.seed(seed$seed) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
rm_seed <- function() { |
| 131 |
if (!has_seed()) { |
| 132 |
return(NULL) |
| 133 |
} |
| 134 |
set.seed(seed = NULL) # also reset Normal RNG state (cf. #162) |
| 135 |
rm(".Random.seed", envir = globalenv()) |
| 136 |
} |