Fixing set.seed for an entire session

后端 未结 6 1602
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 01:59

I am using R to construct an agent based model with a monte carlo process. This means I got many functions that use a random engine of some kind. In order to get reproducibl

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 02:22

    You could do a wrapper function, like so:

    > wrap.3.digit.sample <- function(x) {
    +    set.seed(123)
    +    return(sample(x, 3))
    + }
    > wrap.3.digit.sample(c(1:10))
    [1] 3 8 4
    > wrap.3.digit.sample(c(1:10))
    [1] 3 8 4
    

    There is probably a more elegant way, and I'm sure someone will chime in with it. But, if they don't, this should make your life easier.

提交回复
热议问题