I have noticed that if one uses another pseudo-random number generator when generating a pseudo-random sequence, the seed sequence is interfered. My question is if there is anything to do about it? Can you somehow ensure original seed sequence is continued? Let me show an example;
A simple for-loop which prints a pseudo-random number drawn from the normal distribution:
set.seed(145)
for (i in 1:10){
print(rnorm(1,0,1))
}
Which gives the following output:
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 1.370344
[1] 0.5277918
[1] 0.4030967
[1] 1.167752
[1] 0.7926794
Next, we introduce a pseudo-random draw from the uniform distribution if the iterator is equal to five.
set.seed(145)
for (i in 1:10){
print(rnorm(1,0,1))
if (i == 5){
print(runif(1,0,1))
}
}
Which gives the following output (in the following output, the star marks the pseudo-random draw from the uniform distribution):
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 0.9147102*
[1] -1.508828
[1] -0.03101992
[1] -1.091504
[1] 0.2442405
[1] -0.6103299
What I try to seek an answer on, is whether or not it is possible to continue the original seed sequence introduced by set.seed(145), and thereby get the following output:
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 0.9147102*
[1] 1.370344
[1] 0.5277918
[1] 0.4030967
[1] 1.167752
[1] 0.7926794
Every input is highly appreciated, especially some references to literature on this specific issue.
EDIT:
Based on the input of Rui Barradas I tried to implement it in a function of my own but without luck. Besides the rnorm sampling within each iteration of the for-loop, there shouldn't be any other randomness in the for-loop expect for the sampling in the if-statement which should be handled by the fix of Rui. But unfortunately there seems to be something interfering the seed sequence, as the two functions below does not return the same, and they are equal with the exception of how the randomness (epsilon normally in AR-1 equation) is drawn.
tt <- rnorm(500,0,1)*10
test1 <- function(y, x0=1, n,qsigma = 3, alpha = 5, beta = 20, limit = 0.30){
t <- length(y)
gama <- (alpha + beta)/2
x <- matrix(0,n,t)
x[, 1] <- rep(x0,n)
for(s in 2:t) {
x[, s] <-pmax(alpha*(x[,s-1]<=gama) +beta*(x[,s-1]>gama)+rnorm(n,0,qsigma),1)
if (s==250) {
current <- .GlobalEnv$.Random.seed
resamp <- sample(n, n, replace = TRUE)
x[,s] <- x[resamp,s]
.GlobalEnv$.Random.seed <- current
}
}
list(x = x)
}
test3 <- function(y, x0=1, n,qsigma = 3, alpha = 5, beta = 20, limit = 0.30) {
t <- length(y)
gama <- (alpha + beta)/2
x <- matrix(0,n,t)
x[, 1] <- rep(x0,n)
e_4 <- matrix(rnorm(n * (t), 0, qsigma),n, (t))
for(s in 2:t) {
x[, s] <-pmax(alpha*(x[,s-1]<=gama) +beta*(x[,s-1]>gama)+e_4[,(s-1)],1)
if (s==250) {resamp <-sample(n, n, replace = TRUE)
x[,s] <- x[resamp,s]
}
}
list(x = x, pp = e_4)
}
set.seed(123)
dej11 <- test3(y = tt, n = 5000)$x
set.seed(123)
dej21 <- test1(y = tt, n = 5000)$x
all.equal(dej11,dej21)
I did expect the above to in the end return True instead of a message telling me that the mean relative difference is 1.186448.