Hi All. I have R code whose functionality is being replicated within a C+ program. The outputs are to be compared to validate the conversion somewhat - however (as is always the case) I have stuffed my code with random number calls. Random uniform numbers in C+ are being produced using the (Boost) mersenne-twister generators (mt11213b & mt19937) - which is the default type of generator in R (if I read things correctly). If it was all within R I would just set the seed for reproducibility. Basically - how do I specify in C+ for a set of random uniform numbers such that they are the same as from R? I have considered the possibility of storing/using the R generated random numbers in the C+ version for validation purposes - but there are a lot of them, and that strikes me as a generally ugly way of doing things. thanks in advance C -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Carl Donovan Lecturer in statistics Ph +44 1334 461802 The Observatory Buchanan Gardens University of St Andrews St Andrews Fife KY16 9LZ Scotland
On 12/21/2005 9:47 AM, Carl wrote:> Hi All. > I have R code whose functionality is being replicated within a C+ > program. The outputs are to be compared to validate the conversion > somewhat - however (as is always the case) I have stuffed my code with > random number calls. > > Random uniform numbers in C+ are being produced using the (Boost) > mersenne-twister generators (mt11213b & mt19937) - which is the default > type of generator in R (if I read things correctly). If it was all > within R I would just set the seed for reproducibility. > > Basically - how do I specify in C+ for a set of random uniform numbers > such that they are the same as from R? I have considered the possibility > of storing/using the R generated random numbers in the C+ version for > validation purposes - but there are a lot of them, and that strikes me > as a generally ugly way of doing things.I'd say the only reasonable way to do this is to call the R generators rather than trying to duplicate them. R tries hard to keep its generators consistent from version to version, but if you have an independent implementation of the same algorithm, it's going to be very hard to validate that you've really got things exactly identical. The Writing R Extensions manual tells how to call the R generators from other programs. You can do it without going through interpreted R code, so there shouldn't be much in the way of a performance penalty. Duncan Murdoch
You can use Marsaglia's multiply with carry. I haven't looked at the C code in R recently, but doubt if it has changed. The C code is very neat, using 6 #defines: static const double RANDCONST=2.32830643654e-10; unsigned long zSeed=362436069, wSeed=521288629; #define zNew ((zSeed=36969*(zSeed&65535)+(zSeed>>16))<<16) #define wNew ((wSeed=18000*(wSeed&65535)+(wSeed>>16))&65535) #define IUNIFORM (zNew+wNew) #define UNIFORM ((zNew+wNew)*RANDCONST) #define setseed(A,B) zSeed=(A);wSeed=(B); #define getseed(A,B) A=zSeed;B=wSeed; See Marsaglia's DIEHARD page for more details: http://www.stat.fsu.edu/pub/diehard/ Carl wrote:> Hi All. > I have R code whose functionality is being replicated within a C+ > program. The outputs are to be compared to validate the conversion > somewhat - however (as is always the case) I have stuffed my code with > random number calls. > > Random uniform numbers in C+ are being produced using the (Boost) > mersenne-twister generators (mt11213b & mt19937) - which is the default > type of generator in R (if I read things correctly). If it was all > within R I would just set the seed for reproducibility. > > Basically - how do I specify in C+ for a set of random uniform numbers > such that they are the same as from R? I have considered the possibility > of storing/using the R generated random numbers in the C+ version for > validation purposes - but there are a lot of them, and that strikes me > as a generally ugly way of doing things. > > thanks in advance > C >-- Bob Wheeler --- http://www.bobwheeler.com/ ECHIP, Inc. --- Randomness comes in bunches.