Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sample function gives different result in console and in knitted document when seed is set

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

like image 383
jdkhaled Avatar asked Oct 20 '25 15:10

jdkhaled


1 Answers

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().

like image 144
Ralf Stubner Avatar answered Oct 23 '25 06:10

Ralf Stubner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!