Notwithstanding my comment, here’s a brute force way of checking this:
rm(.Random.seed) # if it already exists
makeActiveBinding('.Random.seed',
function () stop('Something touched my seed', call. = FALSE),
globalenv())
This will make .Random.seed
into an active binding that throws an error when it’s touched.
This works but it’s very disruptive. Here’s a gentler variant. It has a few interesting features:
- It allows enabling and disabling debugging of
.Random.seed
- It supports getting and setting the seed
- It logs the call but doesn’t stop execution
- It maintains a “whitelist” of top-level calls that shouldn’t be logged
With this you can write the following code, for instance:
# Ignore calls coming from sample.int
> debug_random_seed(ignore = sample.int)
> sample(5)
Getting .Random.seed
Called from sample(5)
Setting .Random.seed
Called from sample(5)
[1] 3 5 4 1 2
> sample.int(5)
[1] 5 1 2 4 3
> undebug_random_seed()
> sample(5)
[1] 2 1 5 3 4
Here is the implementation in all its glory:
debug_random_seed = local({
function (ignore) {
seed_scope = parent.env(environment())
if (is.function(ignore)) ignore = list(ignore)
if (exists('.Random.seed', globalenv())) {
if (bindingIsActive('.Random.seed', globalenv())) {
warning('.Random.seed is already being debugged')
return(invisible())
}
} else {
set.seed(NULL)
}
# Save existing seed before deleting
assign('random_seed', .Random.seed, seed_scope)
rm(.Random.seed, envir = globalenv())
debug_seed = function (new_value) {
if (sys.nframe() > 1 &&
! any(vapply(ignore, identical, logical(1), sys.function(1)))
) {
if (missing(new_value)) {
message('Getting .Random.seed')
} else {
message('Setting .Random.seed')
}
message('Called from ', deparse(sys.call(1)))
}
if (! missing(new_value)) {
assign('random_seed', new_value, seed_scope)
}
random_seed
}
makeActiveBinding('.Random.seed', debug_seed, globalenv())
}
})
undebug_random_seed = function () {
if (! (exists('.Random.seed', globalenv()) &&
bindingIsActive('.Random.seed', globalenv()))) {
warning('.Random.seed is not being debugged')
return(invisible())
}
seed = suppressMessages(.Random.seed)
rm('.Random.seed', envir = globalenv())
assign('.Random.seed', seed, globalenv())
}
Some notes about the code:
- The
debug_random_seed
function is defined inside its own private environment. This environment is designated by seed_scope
in the code. This prevents leaking the private random_seed
variable into the global environment.
- The function defensively checks whether debugging is already enabled. Overkill maybe.
- Debug information is only printed when the seed is accessed within a function call. If the user inspects
.Random.seed
directly on the R console, no logging occurs.