Re: [Rd] How to seed the R random number generator in C (standalone) with an instance of .Random.seed

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Thu, 04 Aug 2011 15:46:14 -0400

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 04 Aug 2011 - 19:50:06 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 04 Aug 2011 - 22:00:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive