Getting different results with set.seed()

I seem to be getting different results when using set.seed() when I'm using base R vs R Studio. I'm running RStudio Version 1.2.1335

set.seed(1)
sample(20)

Wondering if anyone else can reproduce this issue.

3 Likes

I am also having this issue, but in a slightly different way. I have two different windows of R Studio open (same R studio and same version of R) and am getting different results of set.seed()

with the same code used by @mileschen

2 Likes

I am having the same issue as well!

I'm having the same issue.
R 3.6.0 RStudio 1.2.1335
image

There were some changes in R 3.6.0 related to how sample() works. From https://github.com/wch/r-source/blob/7f6cc784523dfa69087958633f7deb309d9c8718/doc/NEWS.Rd#L150-L161:

\item The default method for generating from a discrete uniform
distribution (used in \code{sample()}, for instance) has been
changed. This addresses the fact, pointed out by Ottoboni and
Stark, that the previous method made \code{sample()} noticeably
non-uniform on large populations. See \PR{17494} for a
discussion. The previous method can be requested using
\code{RNGkind()} or \code{RNGversion()} if necessary for
reproduction of old results. Thanks to Duncan Murdoch for
contributing the patch and Gabe Becker for further assistance.

The output of \code{RNGkind()} has been changed to also return the
\sQuote{kind} used by \code{sample()}.

You may want to double-check what the output of RNGkind() is -- it's possible that you're loading an R package that is changing the requested random number generator.

To compare:

> RNGkind(sample.kind = "Rounding")
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
> set.seed(1); sample(20)
 [1]  6  8 11 16  4 14 15  9 19  1  3  2 20 10  5
[16]  7 12 17 18 13
> 
> RNGkind(sample.kind = "Rejection")
> set.seed(1); sample(20)
 [1]  4  7  1  2 13 19 11 17 14  3 18  5  9 16  6
[16] 15 12 10 20  8
3 Likes

Yes, see the following explanation here on SO. You might need to change some code in order to make results consistent across 3.6.0 and previous versions of R. It's possible that the R instance you're using and what RStudio is using are different R versions.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.