Q: `devtools::test()`, `RNGversion`, and `sample` yields different results Below I present an issue I have with Rstudio's test feature. It is a simplified version of what I do in a package. I surely have done something wrong but I cannot figure out what. I discovered it when I started to make changes on a package on CRAN. Some tests suddenly did not pass in Rstudio but do with R CMD check.
To see the issue, create a new package with Rstudio and call usethis::use_test(). Then edit tests/testthat.R so the file is 
library(testthat)
library(<name-of-package>)

suppressWarnings(RNGversion("3.5.0"))

test_check("<name-of-package>")

Next, go into the tests/testthat/test-hello.R and change it to
set.seed(1)
rnorm(1)

test_that("seed is the same",
          expect_known_value(.Random.seed, "the-seed.RDS"))

test_that("sample is the same",
          expect_known_value(sample(100), "sample.RDS"))

Build the package and then run devtools::test() three times. The first time creates the references values. The second time yields 
devtools::test()
#R Loading <name-of-package>
#R Testing <name-of-package>
#R ✔ |  OK F W S | Context
#R ✖ |   1 1     | hello
#R ──────────────────────────────────────────────────────────────────────
#R ???: failure: sample is the same
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 100/100 mismatches (average diff: 37.1)
#R [1] 39 - 76 == -37
#R [2]  1 - 39 == -38
#R [3] 34 - 24 ==  10
#R [4] 87 - 53 ==  34
#R [5] 43 - 92 == -49
#R [6] 14 - 86 == -72
#R [7] 82 - 40 ==  42
#R [8] 59 - 83 == -24
#R [9] 51 - 90 == -39
#R ...
#R ──────────────────────────────────────────────────────────────────────
#R 
#R ══ Results ═══════════════════════════════════════════════════════════
#R OK:       1
#R Failed:   1
#R Warnings: 0
#R Skipped:  0

and overrides the test results. Everything passes the third time. Running tests through Rstudio with Build > More > Test Package yields no failures. Next, go to the console, then to the parent directory and run
R CMD build <name-of-package>
R CMD check <name-of-package>_0.1.0.tar.gz

The latter yields the following testthat.Rout.fail
#R R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
#R Copyright (C) 2019 The R Foundation for Statistical Computing
#R Platform: x86_64-pc-linux-gnu (64-bit)
#R 
#R R is free software and comes with ABSOLUTELY NO WARRANTY.
#R You are welcome to redistribute it under certain conditions.
#R Type 'license()' or 'licence()' for distribution details.
#R 
#R R is a collaborative project with many contributors.
#R Type 'contributors()' for more information and
#R 'citation()' on how to cite R or R packages in publications.
#R 
#R Type 'demo()' for some demos, 'help()' for on-line help, or
#R 'help.start()' for an HTML browser interface to help.
#R Type 'q()' to quit R.
#R 
library(testthat)
library(<name-of-package>)

suppressWarnings(RNGversion("3.5.0")) 

test_check("<name-of-package>")
#R ── 1. Failure: seed is the same  ─────────────────────────────────────
#R `.Random.seed` has changed from known value recorded in 'the-seed.RDS'.
#R 1/626 mismatches
#R [1] 403 - 10403 == -10000
#R 
#R ── 2. Failure: sample is the same  ────────────────────────────────────
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 99/100 mismatches (average diff: 36.6)
#R [1] 58 - 39 ==  19
#R [2] 90 -  1 ==  89
#R [3] 20 - 34 == -14
#R [4] 88 - 87 ==   1
#R [5] 91 - 43 ==  48
#R [6] 63 - 14 ==  49
#R [7] 60 - 82 == -22
#R [8]  6 - 59 == -53
#R [9] 19 - 51 == -32
#R ...
#R 
#R ══ testthat results  ═════════════════════════════════════════════════
#R OK: 0 SKIPPED: 0 WARNINGS: 0 FAILED: 2
#R 1. Failure: seed is the same 
#R 2. Failure: sample is the same 
#R 
#R Error: testthat unit tests failed
#R Execution halted

Restarting Rstudio and running 
setwd("tests")
source('path/to/package/<name-of-package>/tests/testthat.R')

Yields the same as R CMD check. What I get with R CMD check and by sourcing the testthat.R file in my original package is the same as the old reference values. Thus, it seems like something is happening with devtools::test(). Here is my sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] <name-of-package>_0.1.0 testthat_2.1.1  

loaded via a namespace (and not attached):
 [1] compiler_3.6.0   assertthat_0.2.1 magrittr_1.5    
 [4] R6_2.4.0         cli_1.1.0        tools_3.6.0     
 [7] withr_2.1.2      rstudioapi_0.10  yaml_2.2.0      
[10] crayon_1.3.4     rlang_0.3.4  


Update
A solution suggested here is to create a file which comes first in alphabetic order than all the tests in tests/testthat (thus it will be run first, see ?testthat::source_file). 
So the error can be fixed by repeating the steps in my question and by replacing 

Then edit tests/testthat.R so the file is
library(testthat)
library(<name-of-package>)

suppressWarnings(RNGversion("3.5.0"))
test_check("<name-of-package>")


with creating a file tests/testthat/helper.R with this content
suppressWarnings(RNGversion("3.5.0"))

For some reason, this still yields a different result between the first and second devtools::test() call but now everything else yields the same. The output from the second devtools::test() call is
devtools::test()
#R Loading wtfpackage
#R Testing wtfpackage
#R ✔ |  OK F W S | Context
#R ✖ |   1 1     | hello
#R ────────────────────────────────────────────────────────────────────
#R ???: failure: sample is the same
#R sample(100) has changed from known value recorded in 'sample.RDS'.
#R 99/100 mismatches (average diff: 34.6)
#R [1] 58 - 28 ==  30
#R [2] 90 - 99 ==  -9
#R [3] 20 - 63 == -43
#R [4] 88 - 21 ==  67
#R [5] 91 - 13 ==  78
#R [6] 63 - 46 ==  17
#R [7] 60 - 87 == -27
#R [8]  6 - 56 == -50
#R [9] 19 - 90 == -71
#R ...
#R ────────────────────────────────────────────────────────────────────
#R 
#R ══ Results ═════════════════════════════════════════════════════════
#R OK:       1
#R Failed:   1
#R Warnings: 0
#R Skipped:  0

