Constantin
Constantin

Reputation: 21

Why Rmarkdown shows different random numbers in pdf output than the ones in the Rmd file?

I set.seed in Rmd file to generate random numbers, but when I knit the document I get different random numbers. Here is a screen shot for the Rmd and pdf documents side by side. Rmd and pdf side by side

Upvotes: 0

Views: 1791

Answers (1)

Ralf Stubner
Ralf Stubner

Reputation: 26823

In R 3.6.0 the internal algorithm used by sample() has changed. The default for a new session is

> set.seed(2345)
> sample(1:10, 5)
[1]  3  7 10  2  4

which is what you get in the PDF file. One can manually change to the old "Rounding" method, though:

> set.seed(2345, sample.kind="Rounding")
Warning message:
In set.seed(2345, sample.kind = "Rounding") :
  non-uniform 'Rounding' sampler used
> sample(1:10, 5)
[1]  2 10  6  1  3

You have at some point made this change in your R session, as can be seen from the output of sessionInfo(). You can either change this back with RNGkind(sample.kind="Rejection") or by starting a new R session.

BTW, in general please include code samples as text, not as images.

Upvotes: 3

Related Questions