[R] Random Seed Location

Paul Gilbert pgilbert902 at gmail.com
Mon Mar 5 22:40:46 CET 2018



On 03/04/2018 07:14 PM, Henrik Bengtsson wrote:
> On Sun, Mar 4, 2018 at 3:23 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
...
>> An issue is that .Random.seed doesn't contain the full state of the RNG
>> system, so restoring it doesn't necessarily lead to an identical sequence of
>> output.  The only way to guarantee the sequence will repeat is to call
>> set.seed(n), and that only leads to a tiny fraction of possible states.
>>
>> Expanding .Random.seed so that it does contain the full state would be a
>> good idea, and that would make your preserveRandomSeed really easy to write.
>>
>> Here's a demo that .Random.seed is not enough:
>>
>>> set.seed(123, normal.kind = "Box-Muller")
>>> rnorm(1)
>> [1] -0.1613431
>>> save <- .Random.seed
>>> rnorm(1)
>> [1] 0.6706031
>>> .Random.seed <- save
>>> rnorm(1)
>> [1] -0.4194403
>>
>> If .Random.seed were the complete state, the 2nd and 3rd rnorm results would
>> be the same.

To be pedantic, it is not the RNG state that is the problem, it is the 
state of the normal transformation "Box-Muller".  And, again pedantic

 >So, this is is only for some of the RNG kinds.

As I recall, it is not a problem for any RNG kinds, it is only a problem 
with the Box-Muller normal.kind. Things may have changed, and parallel 
adds the need to record number of nodes.

>Is the reason for this
> limitation that it is not possible for R (not even the R internals) to
> get hold of some of the RNG generators?  In other words, it is
> unlikely to ever be fixed?

It has been around for a very long time (since at least R 0.99) and, as 
I recall, it was not easy to fix. I think the more substantial reason is 
that Box-Muller is no longer the default or preferred normal generator. 
It may only be used for backward compatibility, in which case messing 
with it could be a disaster with very little potential benefit.

Paul



More information about the R-help mailing list