We can use set.seed()
to set a random seed in R, and this has a global effect. Here is a minimal example to illustrate my goal:
set.seed(0)
runif(1)
# [1] 0.8966972
set.seed(0)
f <- function() {
# I do not want this random number to be affected by the global seed
runif(1)
}
f()
# [1] 0.8966972
Basically I want to be able to avoid the effect of the global random seed (i.e., .Random.seed
) in a local environment, such as an R function, so that I can achieve some sort of randomness over which the user has no control. For example, even if the user has set.seed()
, he will still get different output every time he calls this function.
Now there are two implementations. The first one relies on set.seed(NULL)
to let R re-initialize the random seed every time I want to get some random numbers:
createUniqueId <- function(bytes) {
withPrivateSeed(
paste(as.hexmode(sample(256, bytes, replace = TRUE) - 1), collapse = "")
)
}
withPrivateSeed <- function(expr, seed = NULL) {
oldSeed <- if (exists('.Random.seed', envir = .GlobalEnv, inherits = FALSE)) {
get('.Random.seed', envir = .GlobalEnv, inherits = FALSE)
}
if (!is.null(oldSeed)) {
on.exit(assign('.Random.seed', oldSeed, envir = .GlobalEnv), add = TRUE)
}
set.seed(seed)
expr
}
You can see I get different id strings even if I set the seed to 0, and the global random number stream is still reproducible:
> set.seed(0)
> runif(3)
[1] 0.8966972 0.2655087 0.3721239
> createUniqueId(4)
[1] "83a18600"
> runif(3)
[1] 0.5728534 0.9082078 0.2016819
> set.seed(0)
> runif(3) # same
[1] 0.8966972 0.2655087 0.3721239
> createUniqueId(4) # different
[1] "77cb3d91"
> runif(3)
[1] 0.5728534 0.9082078 0.2016819
> set.seed(0)
> runif(3)
[1] 0.8966972 0.2655087 0.3721239
> createUniqueId(4)
[1] "c41d61d8"
> runif(3)
[1] 0.5728534 0.9082078 0.2016819
The second implementation can be found here on Github. It is more complicated, and the basic idea is:
- initialize the random seed during package startup using
set.seed(NULL)
(in.onLoad()
) - store the random seed in a separate environment (
.globals$ownSeed
) - each time when we want to generate random numbers:
- assign the local seed to the global random seed
- generate random numbers
- assign the new global seed (it has changed due to step 2) to the local seed
- restore the global seed to its original value
Now my question is if the two approaches are equivalent in theory. The randomness of first approach relies on the current time and process ID when createUniqueId()
is called, and the second approach seems to rely on the time and process ID when the package is loaded. For the first approach, is it possible that two calls of createUniqueId()
happen exactly at the same time in the same R process so that they return the same id string?
Update
In the answer below, Robert Krzyzanowski provided some empirical evidence that set.seed(NULL)
can lead to serious ID collisions. I did a simple visualization for it:
createGlobalUniqueId <- function(bytes) {
paste(as.hexmode(sample(256, bytes, replace = TRUE) - 1), collapse = "")
}
n <- 10000
length(unique(replicate(n, createGlobalUniqueId(5))))
length(unique(x <- replicate(n, createUniqueId(5))))
# denote duplicated values by 1, and unique ones by 0
png('rng-time.png', width = 4000, height = 400)
par(mar = c(4, 4, .1, .1), xaxs = 'i')
plot(1:n, duplicated(x), type = 'l')
dev.off()
When the line reaches the top of the plot, that means there is a duplicate value generated. However, note these duplicates do not come successively, i.e. any(x[-1] == x[-n])
is normally FALSE
. There might be a pattern for the duplication associated with the system time. I'm not able to investigate further due to my lack of understanding of how the time-based random seed works, but you can see the relevant pieces of C source code here and here.
set.seed(NULL)
in the post, which does what you suggested, and is actually even better, since it makes use of the process ID as well. I think this should be the answer of the post that you linked to. The question is whether the two approaches differ in theory.