multiple random number sequences

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
#

Hi @smayekawa ,

this would be my approach:

  • make the main matrix and the use it in both methods (without modifing it - just call the columns you need)
  • run analysis 1 since it doesn't invokes any random number generation calling the main matrix
  • for analysis 2 just use a different seed - this way it still keeps beeing reproducible. Since the main matrix is fixed you would not encounter any changes beacuse of the the new generator (and would avoid the reseting of the global seed). If you have to make changes to the original matrix in the second analysis - make a copy of the original matrix and do the calculation on the new matrix.

Example code:

testRN0 <- function(seed, seedMethodB){
  
  # prepare for both runs
  set.seed(seed)
  RN = matrix(0,2,15)
  for( rep in 1:2 ){
    # sample osb id 
    rn=sample(0:9,15, replace=TRUE)
    # set the datapoints
    RN[rep,]=rn
  }
  
  # run analysis 1
  # since this doesn't use any random number generation but just 
  # the matrix above procceed as usual
  

  # analysis by method B
  set.seed(seedMethodB)
  # This way your second random number generator can still be reproduced
  # but has no effect on the original "RN" matrix since this one is set 
  # at the begining
  # run analysis 2
  
} # end of testRN0

# example call
testRN0(1701, 314)

Thank you.
Saving the original data frame (or row id) should work w/o any problem.

Do you know why we should avoid modifying global .Random.seed?
What kind of problem do you anticipate?
I'm just curious.

I know the user manual of the core team is very technical but reading Random numbers - Writing R Extensions shows you that the value of .Random.seed is directly linked to some low level C code which is behind all the random number generation done in R .

So if you don't know exactly what you are doing and how your calls will influence every other subsequent call to other low level functions including random number generation , your analysis can suffer of some unwanted repetition or unexpected number generation which you can't replicate (or worse - understand).

I would always stick to the helper functions like set.seed which are tested for decades now and where i can cite, link ressources or test it against some well know examples.

1 Like

Thank you vedoa.
At first, I thought that set.seed(.Randm.seed) may recover the original sequence
from which .Random.seed was saved but it did not.

Anyway, it seems a good practice not to use the methods which are not recommended.

@smayekawa your welcome, if it adressed your question properly please consider closing the issue.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.