4

I'm creating a document in a Rmarkdown file and knitting to HTML for file submission. Generating seeded samples using the sample function provide different results in the console and the knitted file.

I am using R Studio version 1.0.153 and R 3.6.0

edit: I have updated R Studio to version 1.2.1335 and am still having this issue

set.seed(1)
rnorm(1)
sample(1:10, 1)

in the console and knitted file, the value for rnorm(1) is the same, however, in the console, I see that I have sampled 6 in the console, and 7 in the knitted document

jdkhaled
  • 43
  • 1
  • 6

1 Answers1

8

R 3.6.0 changed the sampling method. With the new method (default or Rejection) I get 7. With the old one I get 6:

set.seed(1)
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 7

set.seed(1, sample.kind = "Rounding")
#> Warning in set.seed(1, sample.kind = "Rounding"): non-uniform 'Rounding'
#> sampler used
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 6

Created on 2019-05-23 by the reprex package (v0.2.1)

So it seems you have somehow set sample.kind = "Rounding" in the console. You can check this from the output of RNGkind().

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • 2
    Thank you so much! This was exactly correct. `sessionInfo()` as @Russ mentioned showed non-default options (rounding) in the console, but nothing in the knit document. I was able to change it back by setting `RNGkind(sample.kind = "Rejection")` – jdkhaled May 23 '19 at 18:47