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?