R ignores setting .Random.seed
inside of an lapply. Using set.seed
however, works fine.
Some code:
# I can save the state of the RNG for a few seeds
seed.list <- lapply( 1:5, function(x) {
set.seed(x)
seed.state <- .Random.seed
print( rnorm(1) )
return( seed.state )})
#[1] -0.6264538
#[1] -0.8969145
#[1] -0.9619334
# But I get different numbers if I try to restore
# the state of the RNG inside of an lapply
tmp.rest.state <- lapply(1:5, function(x) {
.Random.seed <- seed.list[[x]]
print(rnorm(1))})
# [1] -0.2925257
# [1] 0.2587882
# [1] -1.152132
# lapply is just ignoring the assignment of .Random.seed
.Random.seed <- seed.list[[3]]
print( rnorm(1) ) # The last printed value from seed.list
# [1] -0.9619334
print( rnorm(1) ) # The first value in tmp.rest.state
# [1] -0.2925257
My goal is to checkpoint MCMC runs so that they can be resumed exactly. I can easily save the state of the RNG, I just can't get R to load it inside of an lapply loop!
Is there a way to force R to notice setting .Random.seed
? Or is there a simpler way to make this happen?
In case it matters, I'm using 64 bit R:
R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Platform: x86_64-pc-linux-gnu (64-bit)
On Ubuntu 12.04 LTS:
nathanvan@nathanvan-N61Jq:~$ uname -a
Linux nathanvan-N61Jq 3.2.0-26-generic #41-Ubuntu SMP Thu Jun 14 17:49:24 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux