[R] Fwd: Re: .Random.seed for the Mersenne Twister

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Jan 10 02:57:37 CET 2020


Forgot to cc list.


-------- Original Message --------
From: Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Sent: January 9, 2020 2:10:00 PM PST
To: Luca Passalacqua <luca.passalacqua using uniroma1.it>
Subject: Re: [R] .Random.seed for the Mersenne Twister

No information is lost. Thirty-two "one" bits is equally valid when viewed as a -1L in R or as 4294967295 as a uint32 in C. If you need to enter unsigned int 4294967295 into memory using R code for the benefit of some C code, use -1L instead.

If you read the help file recommended by Uwe you will see that the seed vector is the RNG kind value followed by the current position followed by 624 values ("MT").

Be warned that other stochastc functions in R (like rnorm) can add their own algorithms on top of the basic RNG... reproducing others' calculations may require that you re-implement some functionality the way the original authors did it to get the same results.

RNGkind('default')
RNGkind()
#[1] "Mersenne-Twister" "Inversion"      
set.seed(1)
x <- .Random.seed
before <- runif(5)
head(x)
m1 <- 2^31-1
m2 <- 2^32
dput(x[1:2])
index <- x[2]
MT <- as.double( x[-(1:2)] )
MTu <- MT
neg <- MT < 0
MTu[neg] <- MTu[neg] + m2
MT[1:20]
MTu[1:20]
MTs <- MTu
neg2 <- m1 < MTu
MTs[neg2] <- MTs[neg2] - m2
x2 <- c(403L,index,as.integer(MTs))
head(x2)
.Random.seed <- x2
after <- runif(5)
before==after

On January 9, 2020 9:43:22 AM PST, Luca Passalacqua <luca.passalacqua using uniroma1.it> wrote:
>Dear Jeff and Uwe,
>
>  thanks for your kind answers. Restoring works (see below),
>but I do not understand how, since in mapping from unsigned
>to signed, information is lost, and you cannot go back from
>signed to unsigned.
>
>The reason I would like to alter the initial state is that R is
>initialing
>the seed
>with an algorithm different from that used by the MT authors, but I
>would like to use the "original" seed to reproduce their results.
>
>> set.seed(1)
>> s0 = .Random.seed
>> s0[1:10]
> [1]        403        624 -169270483 -442010614 -603558397 -222347416
>1489374793  865871222 1734802815   98005428
>
>> runif(5)
>[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819
>
>> runif(5)
>[1] 0.89838968 0.94467527 0.66079779 0.62911404 0.06178627
>
>> .Random.seed = s0
>> runif(5)
>[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819
>
>Luca
>
>Il giorno gio 9 gen 2020 alle ore 16:44 Jeff Newmiller <
>jdnewmil using dcn.davis.ca.us> ha scritto:
>
>> I am no expert on this specific algorithm, but there is no "32-bit
>> unsigned integer" type in R. Presumably the interpretation of those
>> negative numbers in the C code is as if they were unsigned while R
>presents
>> them as if they were signed because it cannot do otherwise.
>>
>> AFAIK you need to use set.seed to configure .Random.seed, and you can
>> retrieve and later restore the vectors created this way in the
>future. As I
>> understand it there exist invalid vectors that cannot arbitrarily be
>used
>> by this algorithm so generating them yourself is at the very least
>hard,
>> and possibly could break in future versions of R.
>>
>> On January 9, 2020 1:18:01 AM PST, Luca Passalacqua via R-help <
>> r-help using r-project.org> wrote:
>> >Dear R users,
>> >
>> > inspecting  .Random.seed for the Mersenne Twister (MT) I find
>(many)
>> >negative values for the
>> >624 values of the initial state of the generator.
>> >It seems to me that this is a bug (an unsigned integer mapped to a
>> >signed
>> >integer ?),
>> >since, to my understanding, the R version of MT should be working
>with
>> >32-bits unsigned long.
>> >Moreover, this prevents starting the generator by setting
>.Random.seed
>> >to
>> >user provided
>> >values.
>> >Could someone please provide some insight to this issue ?
>> >Many thanks,
>> >
>> >Luca Passalacqua
>> >
>> >
>> >> RNGkind('default')> RNGkind()[1] "Mersenne-Twister" "Inversion"
>> >> set.seed(1)> .Random.seed  [1]         403         624  -169270483
>> >-442010614  -603558397  ...
>>
>> --
>> Sent from my phone. Please excuse my brevity.
>>

-- 
Sent from my phone. Please excuse my brevity.
-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list