[R] system time - windows specific problem

From: Christophe Dutang <christophe.dutang_at_wanadoo.fr>
Date: Fri, 18 Jul 2008 23:26:37 +0200


Hi all,

I'm currently implementing quasi random generation (torus) on R (package randtoolbox available on CRAN). Even if it is not a great idea, I decided to use the machine time to initiate the seed. So when the seed is not specified by the user, the pkg uses the time machine time.
Hence the following R code should produce different uniform variates :

 > for(i in 1:10)print(torus(1))

But on windows, I get :
 > library(randtoolbox)
 > for(i in 1:10)print(torus(1))
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116
[1] 0.1313116

 >

while on mac os, we get :
 > library(randtoolbox)
 > for(i in 1:10)print(torus(1))
[1] 0.9730577
[1] 0.9124289
[1] 0.5123534
[1] 0.4640765
[1] 0.9367557
[1] 0.2945414
[1] 0.3671455
[1] 0.2698379
[1] 0.6036739
[1] 0.6762776

 >

I think I know where the problem is. in the C file, randtoolbox.c, I use the machine time as follows
void randSetSeed()
{

         /* struct timeval {
                 unsigned long tv_sec; // seconds since Jan. 1, 1970
                 long tv_usec; // and microseconds  };
         */

         struct timeval tv;

         //take the machine time
         gettimeofday (&tv, NULL);

         // [ 2^16 * microsecond ] xor [ second ]
         seed = ((unsigned long long) tv.tv_usec << 16) ^ tv.tv_sec;

         isInit = 1;

}
and the C function randSetSeed is called. (in the header file, I include <sys/time.h>).
I think on windows there is no micro second precision and so the "randomize" seed is the same between two consecutive calls.

I try many things to deal with this problem (for example include <windows.h> and use windows specific time function), but it does not work. I'm asking for help on R mailing list because what solve on R for the runif function.

You could answer me to go to the R source. Of course, I look there my code is based on this code. But in the RNG.c file which implements random number generation. they use line 271-289

#include <time.h>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif

static void Randomize(RNGtype kind)
{
/* Only called by GetRNGstate() when there is no .Random.seed */

     Int32 seed;
#if HAVE_GETTIMEOFDAY

   {

     struct timeval tv;
     gettimeofday (&tv, NULL);
     seed = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
   }
#elif HAVE_TIME

     seed = (Int32) time(NULL);
#else

     /* unlikely, but use random contents */ #endif

     srand(seed);

     RNG_Init(kind, seed);
}

If I try to use directly the time function on windows rather than gettimeofday (which should not be on windows) but it does not solve the problem.

I'm wondering how R has solved this issue?

That's where I'm asking for any help.

Thanks in advance

Christophe

PS : I use in both examples R 2.7.1 first on windows XP, second on mac os leopard.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 19 Jul 2008 - 01:19:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 19 Jul 2008 - 12:32:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive