Argument of set.seed in R

后端 未结 1 813
梦如初夏
梦如初夏 2021-02-15 05:12

I am trying to understand how set.seed works in R. I understand it, can reproduce random samples, but I don\'t know what is the difference between set.seed(1)

1条回答
  •  礼貌的吻别
    2021-02-15 05:38

    The seed argument in set.seed is a single value, interpreted as an integer (as defined in help(set.seed()). The seed in set.seed produces random values which are unique to that seed (and will be same irrespective of the computer you run and hence ensures reproducibility). So the random values generated by set.seed(1) and set.seed(123) will not be the same but the random values generated by R in your computer using set.seed(1) and by R in my computer using the same seed are the same.

    set.seed(1)
    x<-rnorm(10,2,1)
    > x
     [1] 1.373546 2.183643 1.164371 3.595281 2.329508 1.179532 2.487429 2.738325 2.575781 1.694612
    set.seed(123)
    y<-rnorm(10,2,1)
    > y
     [1] 1.4395244 1.7698225 3.5587083 2.0705084 2.1292877 3.7150650 2.4609162 0.7349388 1.3131471 1.5543380
    
    > identical(x,y)
    [1] FALSE
    

    0 讨论(0)
提交回复
热议问题