[R] random number generation issues with r and compiled C code
Faheem Mitha
faheem at email.unc.edu
Thu Jan 24 23:39:14 CET 2002
Dear People,
I have been writing a simulation routine that is currently entirely
written in c++. I've been using the R standalone math library to give me
random number generation for this. However, I have had to set the seed
myself, and I am using
set_seed(time(NULL), clock)
for every call to unif_rand().
However, this works really badly. The random numbers aren't uniform at
all. Does anyone have better suggestions?
I think the problem is that every time I call the random number generation
function, I have to set the seed, and this messes up the uniformity of the
random number generation. To be more explicit, if I was to call the seed
once, and then generate a thousand unif rand variates, these would be
distributed more or less correctly, but if I set the seed for each of the
thousand variates separately, this screws up the algorithm. Is my
understanding correct?
I've been looking on the web, and it looks like there is no perfect
solution to the problem of generating random numbers in C/C++. So, I'm
thinking of running my program inside R by compiling my code as a shared
library, and using R's builtin random number facilities.
I have a couple of questions about this.
1) The functions GetRNGstate() and PutRNGetate() read and write
.Random.Seed. The question is what is the best strategy for calling these.
Would it be best to call these routines as infrequently as possible? My
remarks above (if correct) suggest that. That is, would be best to call
GetRNGstate() once before generating all random numbers necessary and then
call it again when I have finished calling all the random variates? Or
does it not matter that much?
Also, is scoping a problem here? I have not tried this yet, but suppose I
was to do as follows
foo()
{
GetRNGstate();
randcall();
randcall();
PutRNGetate();
}
where
randcall()
{
int i;
for(i=0; i < 100; i++)
cout << unif_rand();
}
would this be Ok, or do calls to GetRNGstate() and PutRNGetate() have
to be inside randcall()? If this is the case, then the above strategy
of reading in seed as early as possible and writing it out as late as
possible will not work.
Also, what is the answer to the corresponding question in the purely
C/C++ situation? Ie. can I call set_seed at top level? eg, in main()?
If so, this would make a lot of the problems go away.
2) What is the difference between doing
GetRNGstate();
unif_rand();
PutRNGetate();
and calling runif()?
or are these equivalent? Ie. does runif() read in and out seed itself?
Thanks in advance.
Sincerely, Faheem Mitha.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list