I am looking for a method to generate random numbers using multiple seed sequences.
For example, consider the situation where you are doing some simulation study.
You repeat the following:
analysis 1
1 set seed.
2 generate a traning data set by resampling.
3 analyze the data set by method A.
4 accumulate the result.
5 repeat above 2 to 4.
Now, suppose method A does not use randon munbers in it but
method B uses sone random numbers in it.
Then, the data set generated above may be different from the ones
from below:
analysis 2
1 set seed.
2 generate a traning data set by resampling.
3 analyze the data set by method B (with or withoug setting seed).
4 accumulate the result.
5 repeat above 2 to 4.
Since in order to compare the method, we need to analyse the same data set
generated at the step 2 of the above analyses, we must use two random number
sequences in analysis 2: one with the original seed and one with another in method B.
Or I need to resume the original sequence after another sequence was invoked.
I came up with a method which enables above by modifying .Random.seed in the global envionment, but R help says it is not desirable to modify it.
Can anyone help me to do this.
#
# the code starts here.
#
testRN0 <- function( seed, restore=0 ){
# analysis by method A
set.seed(1701)
RNA=matrix(0,2,15)
for( rep in 1:2 ){
# sample osb id
rn=sample(0:9,15, replace=TRUE)
RNA[rep,]=rn
#
# analyze rs1 by method A and accumulate here.
#
} # end of rep lool
# analysis by method B
set.seed(1701)
RNB=matrix(0,2,15)
for( rep in 1:2 ){
# sample osb id
rn=sample(0:9,15, replace=TRUE)
RNB[rep,]=rn
# save the random numbse seed
.R.s=.Random.seed
#
# analyze rs1 by method B and accumulate here.
#
set.seed( 1234 )
temp=sample( 0:9,10)
if( restore ){
# restore random numbse seed
.Random.seed <<- .R.s
}
} # end of rep lool
print(RNA)
print(RNB)
} # end of testRN0
testRN0( 1701 )
testRN0( 1701, restore=1 )
#
# end of code
#