[R] How to construct a valid seed for l'Ecuyer's method with given .Random.seed?
Marius Hofert
marius.hofert at math.ethz.ch
Wed Jan 23 10:01:11 CET 2013
Since clusterSetupRNG() calls clusterSetupRNGstream() and this calls .lec.SetPackageSeed(), I could further minimalize the problem:
set.seed(1)
RNGkind("L'Ecuyer-CMRG") # => .Random.seed is of length 7 (first number encodes the rng kind)
(seed <- .Random.seed[2:7]) # should give a valid seed for l'Ecuyer's RNG
require(rlecuyer) # latest version 0.3-3
.lec.SetPackageSeed(seed)
The last line fails with:
,----
| Error in .lec.SetPackageSeed(seed) :
| Seed[0] >= -767742437, Seed is not set.
`----
Looking at .lec.SetPackageSeed, "seed" seems to pass .lec.CheckSeed() [the check
could probably be improved here (but further checking is done in the C code; see
below)]:
,----
| > .lec.SetPackageSeed
| function (seed = rep(12345, 6))
| {
| if (!exists(".lec.Random.seed.table", envir = .GlobalEnv))
| .lec.init()
| seed <- .lec.CheckSeed(seed) # => bad check since it's passed
| .Call("r_set_package_seed", as.double(seed), PACKAGE = "rlecuyer") # => this fails!
| return(seed)
| }
| <environment: namespace:rlecuyer>
`----
Going into the C code, r_set_package_seed calls RngStream_SetPackageSeed which in turn calls CheckSeed(seed). The relevant part of CheckSeed is this:
,----
| for (i = 0; i < 3; ++i) {
| if (seed[i] >= m1) {
| /* HS 01-25-2012 */
| error("Seed[%1d] >= %d, Seed is not set.\n", i,m1);
| /* original code
| fprintf (stderr, "****************************************\n"
| "ERROR: Seed[%1d] >= m1, Seed is not set.\n"
| "****************************************\n\n", i);
| return (-1);
| */
| }
| }
`----
Note that seed[0] in this (C-)code corresponds to the first element of my
variable "seed", which is -1535484873. This should definitely be smaller than m1
since m1 is defined as 4294967087.0 on line 34 in ./src/RngStream.c of the package 'rlecuyer'.
Why is seed[i] then >= m1??? This is strange. Indeed, as you can see from the
error message above, m1 is taken as -767742437 in my case (why?). Still (and even more
confusing), -1535484873 >= -767742437 is FALSE (!) but the if(){} is entered
anyways...
Cheers,
Marius
More information about the R-help
mailing list