Hi 

I'd like to generate thousands of normal numbers from my C function using 
the C API functions provided R. I have two options:

1. double norm_rand(); (page 61 of R extension 1.8.1)
2. double rnorm(double mu, double sigma); (page 58 of R extension 1.8.1)

If my understanding of R-exts is correct, then I only need to call 
GetRNGstate once, and then call 1000 norm_rand, and then call 
PutRNGstate once for the 1st option.

For the 2nd option, I have to call 1000 times for each of GetRNGstate, 
rnorm, and PutRNGstate.

The pseudo-code for option 1 will be:

Method 1:

GetRNGstate();
for(i=1;i<1000;i++){
   x[i]=norm_rand();
}
PutRNGstate();

The pseudo code for option 2 will be:

Method 2:

for(i=1;i<1000;i++){
   GetRNGstate();
   x[i]=rnorm(0,1);
   PutRNGstate();
}

Of course, I can also write a slower version for option 1, i.e. call 
GetRNGstate and PutRNGstate each time for norm_rand.

method 3:

for(i=1;i<1000;i++){
   GetRNGstate();
   x[i]=norm_rand(); 
   PutRNGstate();
}


My questions are:

1. Are the three methods all correct for generating random numbers?

2. Are they generating the exactly the same random number if we have the 
same random seed?

I searched the R help and google and I didn't find answers. The reason for 
to ask is that if both of the above answers are right, then I'd better 
off use the method 1, which is the fastest as I have to generate 
hundreds thousands of normal numbers.

Thanks!

Yongchao


p.s. please cc to me as I am not on the online list, only on the 
daily digest list.

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to