[R] system time - windows specific problem
Christophe Dutang
christophe.dutang at wanadoo.fr
Fri Jul 18 23:26:37 CEST 2008
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.
More information about the R-help
mailing list