Re: [R] system time - windows specific problem

2008-07-19 Thread Christophe Dutang
Firstly thank you for answering so quickly. I hope I used the good  
mailing list!


Secondly, I check that I make call to the 'randomize' function.  
anyway, I will look more closely to the R source and also try  
QueryPerformanceCounter.


Thirdly, maybe I will remove time machine for quasi random generation  
and use it only for pseudo random generation as in Rmetric packages.  
(but the problem with the seed still persists).


Thanks again.

Christophe

Le 19 juil. 08 à 12:32, Duncan Murdoch a écrit :



On 18/07/2008 5:26 PM, Christophe Dutang wrote:

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 ).
I think on windows there is no micro second precision and so the   
"randomize" seed is the same between two consecutive calls.


The same will eventually be true on other machines, when they are  
fast enough to evaluate two calls during the same microsecond.  (I'd  
guess they're already fast enough if you put that loop in C code  
instead of R code.)  But why would this ever happen?  Surely your  
code doesn't make this call for every random number call; like R, it  
should make it at most once per session.


There is a higher resolution counter in Windows (see  
QueryPerformanceCounter) if you really need it, and you may have  
access to the hardware cycle counter if you need even better  
resolution.  But I don't see why you would need these.


Duncan Murdoch

I try many things to deal with this problem (for example include   
 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 
#ifdef HAVE_SYS_TIME_H
# include 
#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@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.





__
R-help@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.


Re: [R] system time - windows specific problem

2008-07-19 Thread Duncan Murdoch

On 18/07/2008 5:26 PM, Christophe Dutang wrote:

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 ).
I think on windows there is no micro second precision and so the  
"randomize" seed is the same between two consecutive calls.




The same will eventually be true on other machines, when they are fast 
enough to evaluate two calls during the same microsecond.  (I'd guess 
they're already fast enough if you put that loop in C code instead of R 
code.)  But why would this ever happen?  Surely your code doesn't make 
this call for every random number call; like R, it should make it at 
most once per session.


There is a higher resolution counter in Windows (see 
QueryPerformanceCounter) if you really need it, and you may have access 
to the hardware cycle counter if you need even better resolution.  But I 
don't see why you would need these.


Duncan Murdoch

I try many things to deal with this problem (for example include  
 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 
#ifdef HAVE_SYS_TIME_H
# include 
#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@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.


__
R-help@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.


[R] system time - windows specific problem

2008-07-18 Thread Christophe Dutang

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 ).
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  
 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 
#ifdef HAVE_SYS_TIME_H
# include 
#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@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.