I have a question about the random number generator seed number in R. Recently, I am trying to solve the exercises in the book named "An Introduction to Statistical Learning". When I following the practices, I found pretty odd situation. Although I set a same seed number, the result is slightly different from the book.

So, my question is that is it possible that a same seed number results in a different result? I think, it could result from the version of my R (or due to the fact that I am using R studio and the book employed GNU R)

  • 1
    $\begingroup$ Can you refer more precisely to the exercise. Which function or set of commands is generating a different answer? $\endgroup$ Apr 1, 2021 at 7:03
  • $\begingroup$ Given my answer, were the differences of the same order? $\endgroup$
    – Xi'an
    Apr 1, 2021 at 12:33

2 Answers 2


Looking at the first page (first edition) with set.seed() commands,

enter image description here

I was able to reproduce the outcome, using R version 3.4.4 (2018-03-15). (R studio produces exactly the same.)

> set.seed(1303)
> rnorm(10)
[1] -1.1439763145  1.3421293656  2.1853904757  0.5363925179  0.0631929665
[6]  0.5022344825 -0.0004167247  0.5658198405 -0.5725226890 -1.1102250073


> set.seed(3)
> y=rnorm(100)
> mean(y)
[1] 0.01103557
> var(y)
[1] 0.7328675
> sqrt(var(y))
[1] 0.8560768
> sd(y)
[1] 0.8560768

the only difference being the excessive precision in my R output. Note the warning in the text about possible discrepancies occuring with new versions of R.

  • 3
    $\begingroup$ That's a good point. The OP's "slightly different" may simply be different output precisions. +1. $\endgroup$ Mar 31, 2021 at 14:41

The seed should make your analysis fully reproducible. I would much rather assume that differences in R (and package) versions, or a bug in your (or the book's) code, or possibly even machine/OS differences would be responsible for any discrepancies. (Converted from a comment.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.