Is Set.Seed Consistent Over Different Versions of R (And Ubuntu)

Is set.seed consistent over different versions of R (and Ubuntu)?

Cross-OS consistency: yes

If you installed R on two different operating systems without manually changing defaults or the RProfile, you should get the same results when using set.seed().

Consistency over versions of R: not necessarily

It used to be the case that set.seed() would give the same results across R versions, but that's no longer generally true thanks to a little-announced update in R 3.6.0. So you can get cross version consistency comparing results before R 3.6.0, but if you compare a post-3.6.0 use of set.seed() to a pre-3.6.0 use of set.seed(), you will get different results.

You can see that in the examples below:

R 3.2.0

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.5.3

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.6.0

set.seed(1999)
sample(LETTERS, 3)
[1] "D" "Z" "R"

The reason for the inconsistency is that in R 3.6.0, the default kind of under-the-hood random-number generator was changed. Now, in order to get the results from set.seed() to match, you have to first call the function RNGkind(sample.kind = "Rounding").

R 3.6.0

> RNGkind(sample.kind = "Rounding")
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
> set.seed(1999)
> sample(Letters, 3)
[1] "T" "N" "L"

Same seed, different OS, different random numbers in R

From docs:

Random docs:

RNGversion can be used to set the random generators as they were in an earlier R version (for reproducibility).

So try this on all systems:

set.seed(10, kind = "Mersenne-Twister", normal.kind = "Inversion"); rnorm(1)
[1] 0.01874617

R set.seed reproduce by another user on another computer

Well the random number generation depends on the RNG kind used. R uses Mersenne-Twister as the default RNG changing this will change the result not only in different platforms but even on the same computer. For example try doing

RNGkind('Wich')
set.seed(5)
rnorm(5)

You will note that the results you obtain are quite different.

Note that depending on the generator chosen, you will get different results, even on the same computer. Run RNGkind('default') to go back to the default generator.

Notice that numbers generated by a computer are technically not random, but rather pseudo - random. Probably it is possible to generate the exact numbers using the same generator on a different platform/ language. (Still not quite sure about this- Hopefully it will be possible).

I'm using set.seed() but getting different answers in each run

set.seed() is a function expecting a parameter equal to the value you want to seed the pseudorandom number generator(prng) with. The seed is the value used to start the number generation from. Most prng will use the current time as default, but when you pass it a seed you are determining the starting value and therefore all values to come after it as well.

So you need to call it like
set.seed(42) to set your seed appropriately

Here is another question that gives a good response on what this function is actually doing https://stats.stackexchange.com/questions/86285/random-number-set-seedn-in-r

Questions about set.seed() in R

Just for fun:

set.seed.alpha <- function(x) {
require("digest")
hexval <- paste0("0x",digest(x,"crc32"))
intval <- type.convert(hexval) %% .Machine$integer.max
set.seed(intval)
}

So you can do:

set.seed.alpha("hello world")

(in fact x can be any R object, not just an alphanumeric string)



Related Topics



Leave a reply



Submit