1

I have the opposite problem to this questioner: Passing seed/Setting seed/ C within R code . My problem is that I want to generate a random number in C code using the R random number generator, but every time I run it, I get the same "random" value. Here is my code (including the header files I happen to be using, which are not all necessary for the code snippet):

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>   // C header files

#include <R.h>
#include <R_ext/Utils.h>
#include <Rmath.h>

void main(){
GetRNGstate();
double u = unif_rand();
PutRNGstate();
Rprintf("%f\n",u);
}

Now I call it from the R Windows GUI after compiling with R CMD SHLIB and using dyn.load():

> .C("main")
0.000889
list()

And now if I close R, restart, and dyn.load() it again (but without compiling a second time):

> .C("main")
0.000889
list()

According to various bits of code I found on the internet and the "Writing R Extensions" manual, this shouldn't happen. But I'm probably just missing a step?

Community
  • 1
  • 1
Flounderer
  • 640
  • 5
  • 17
  • 1
    This isn't your problem, but (a) there is no standard `` header; the header that declares `malloc` is ``, and (b) `void main()` is wrong, it should be `int main(void)`. `void main()` is frequently indicates a C textbook written by someone who doesn't know C as well as he thinks he does. – Keith Thompson Jan 28 '13 at 04:21
  • 1
    A guess: you have a `set.seed` call in your `.Rprofile` or something similar that's fixing the seed each time you start a new R session? – Kevin Ushey Jan 28 '13 at 08:32
  • @CDRV Unfortunately, it's impossible to tell for certain because someone else's R Profile is automatically loaded when I start R. But it looks very likely that that is the problem! Thank you. Do you happen to know how the random seed can be reset in such a way that it doesn't come out the same every time? – Flounderer Jan 28 '13 at 22:01
  • Never mind, I found it: http://stackoverflow.com/questions/10910698/questions-about-set-seed-in-r – Flounderer Jan 28 '13 at 22:06

1 Answers1

0

I cannot replicate this.

Here is a complete and reproducible example, relying only on the inline package to help with building, compiling, linking and loading a minimal function:

R> library(inline)
R> src <- '
+   GetRNGstate();
+   double u = unif_rand();
+   Rprintf("%f\\n",u);
+   PutRNGstate();
+   return R_NilValue;
+ '
R> fun <- cxxfunction(signature(), body=src)
R> set.seed(42)
R> invisible(fun())   # invisible to suppress the NULL return in foo()
0.914806
R> invisible(fun())
0.937075
R> invisible(fun())
0.286140
R> invisible(fun())
0.830448
R> invisible(fun())
0.641746
R> 

And if I reset the seed, the same sequence starts:

R> set.seed(42)
R> invisible(fun())
0.914806
R> invisible(fun())
0.937075
R> invisible(fun())
0.286140
R> 

You may be loading / running a different function with a side-effect relative to setting the RNG. Also, at just in case that was unclear, your example cannot be loaded as you can't load a function with a main() into R itself.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for trying, but what do you mean "cannot be loaded"? I complied my function using R CMD SHLIB randtest.c, then loaded it into R using dyn.load("randtest.dll"), then ran it by typing .C("main"). It produces the output I gave above. If I close the R GUI, open it again, and reload the thing again, the same value gets printed to the screen. I should have mentioned that I didn't compile the C code again the second time. Unfortunately, I can't use the inline package on this computer. I am using R 2.14-1 in case that helps. – Flounderer Jan 28 '13 at 04:03
  • Wjhat happens when you install `inline` and run my example? As for `main()` maybe you got lucky as yours returns `void` not `int`. Every program can have _exactly one_ main function, and R brings its own. – Dirk Eddelbuettel Jan 28 '13 at 04:09
  • Unfortunately, I can't install anything, including inline, on the computer which I am using. Is there some way of setting the random seed in R to something random, as a workaround? – Flounderer Jan 28 '13 at 21:39
  • Never mind, it looks like the answer is here. http://stackoverflow.com/questions/10910698/questions-about-set-seed-in-r. Many thanks again for your help, and I hope that I will be able to switch to Rcpp soon! – Flounderer Jan 28 '13 at 22:06
  • Just use `set.seed(as.numeric(Sys.time())`. – Dirk Eddelbuettel Jan 28 '13 at 22:08