[Rd] How to seed the R random number generator in C (standalone) with an instance of .Random.seed
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Aug 4 21:46:14 CEST 2011
On 04/08/2011 2:33 PM, oliver ratmann wrote:
> hello all,
>
> I use the R standalone math library in my own C program, and the default R
> random number generator can be seeded with
>
> set_seed(const unsigned int, const unsigned int).
>
> How could I seed the RNG with an instance of .Random.seed ?
>
>
>
> I would need this or a similar workaround for debugging purposes.
>
> More precisely, I use the default R random number generator to sample from
> various distributions in my own C code
>
> SEED<- .Random.seed
> save(SEED, args, file= "last.call.R")
> out<- .Call("my.fun", args)
>
> and can reproduce segmentation faults etc via
>
> load("last.call.R")
> .Random.seed<- SEED
> out<- .Call("my.fun", args)
>
> In order to use valgrind, I wrote a little C program "debug.my.fun" that
> reads in 'args' and 'SEED',
> and then calls "my.fun" (without the SEXP overhead of course).
>
> But the seeds are not set accordingly and segmentation faults are not
> reproducible.
I would guess you are failing to call GetRNGstate() (or possibly
PutRNGstate()) in your C code, so it never sees (or updates) the
.Random.seed value.
Duncan Murdoch
> More precisely, I need to replace the set_seed line in the following snippet
> with something else.
>
> #include<Rmath.h>
> int main(int argc,char* argv[])
> {
> const unsigned int SEED1= 12345, SEED2= 67890;
> set_seed(SEED1, SEED2);
>
> ...
>
> }
>
>
>
>
> Thanks in advance,
> Oliver
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list