I need some random numbers in my C program. Here a small example: //////////////////////////////////////////////////////////////////////////////// #include <R.h> void rand (int* n) { int len = *n; for (int i = 0; i < len; i++) Rprintf("%1.5f ", unif_rand()); } dyn.load("rand.dll") .C("rand", as.integer(10)) dyn.unload("rand.dll") //////////////////////////////////////////////////////////////////////////////// But this gives me 10 times the number 0.00000 (I need 10 numbers between 0 and 1). Can somebody give me a hint what I'm doing wrong (or misunderstand). Thanks in advance Thomas [[alternative HTML version deleted]]
On 11/13/2009 11:41 AM, Thomas Unternaehrer wrote:> I need some random numbers in my C program. Here a small example: > > //////////////////////////////////////////////////////////////////////////////// > #include <R.h> > > void rand (int* n) > { > int len = *n; > for (int i = 0; i < len; i++) > Rprintf("%1.5f ", unif_rand()); > } > > dyn.load("rand.dll") > .C("rand", as.integer(10)) > dyn.unload("rand.dll") > //////////////////////////////////////////////////////////////////////////////// > > But this gives me 10 times the number 0.00000 (I need 10 numbers between 0 > and 1). Can somebody give me a hint what I'm doing wrong (or misunderstand).At a minimum, you forgot to call GetRNGState and PutRNGState. See Writing R Extensions for details. But if you are running this from outside of R, you'll also need to read about building a standalone Mathlib. Duncan Murdoch
Ups... I've overlooked that part. Thanks a lot for the fast response! On Fri, Nov 13, 2009 at 6:05 PM, Duncan Murdoch <murdoch@stats.uwo.ca>wrote:> On 11/13/2009 11:41 AM, Thomas Unternaehrer wrote: > >> I need some random numbers in my C program. Here a small example: >> >> >> //////////////////////////////////////////////////////////////////////////////// >> #include <R.h> >> >> void rand (int* n) >> { >> int len = *n; >> for (int i = 0; i < len; i++) >> Rprintf("%1.5f ", unif_rand()); >> } >> >> dyn.load("rand.dll") >> .C("rand", as.integer(10)) >> dyn.unload("rand.dll") >> >> //////////////////////////////////////////////////////////////////////////////// >> >> But this gives me 10 times the number 0.00000 (I need 10 numbers between 0 >> and 1). Can somebody give me a hint what I'm doing wrong (or >> misunderstand). >> > > At a minimum, you forgot to call GetRNGState and PutRNGState. See Writing > R Extensions for details. But if you are running this from outside of R, > you'll also need to read about building a standalone Mathlib. > > Duncan Murdoch >[[alternative HTML version deleted]]