<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>RE: repeatable random numbers</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">RNGScope() mearly saves the state of the RNG</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> for the R environment</FONT><FONT FACE="Consolas">,</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">return</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">ing</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> it upon</FONT> <FONT FACE="Consolas">class</FONT> <FONT FACE="Consolas">destruction</FONT><FONT FACE="Consolas"> at end of</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">your (compiled)</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">Rcpp function.</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">You can pass</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> a seed to your Rcpp function</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">as an argument</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">and</FONT><FONT FACE="Consolas">,</FONT> <FONT FACE="Consolas">after preserving state with RNGScope(),</FONT><FONT FACE="Consolas"> you can set the seed</FONT> <FONT FACE="Consolas">within</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">the Rcpp function for this repeatable capability. I did this in an example that I</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">listed here</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> a couple of months ago</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> usi</FONT><FONT FACE="Consolas">ng the environment facility to</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Consolas">call R</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">’</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">s</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas"> set.seed command</FONT></SPAN><SPAN LANG="en-us">.</SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">src <- ' </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">Rcpp::RNGScope Scope; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">Environment base("package:base"); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">Function SetSeed = base["set.seed"]; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">int NumRands = 5; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">int NumTri</FONT><FONT FACE="Consolas">als = 3; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">Rcpp::NumericVector RandCol(NumRands); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas"> Rcpp::NumericMatrix RandVals(NumRands, NumTrials*2); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">SetSeed(Named("seed",20) ); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">RandCol=rnorm(NumRands,2,1); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">int j=3; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">for(int i=0; i<NumRands; i++) { </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">RandVals(i,j) = RandCol(i); </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">} </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">return RandVals; </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">' </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas"> fun <- cxxfunction(signature(), </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas"> src, plugin = "Rcpp") </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">test_fun<-fun()</FONT></SPAN><SPAN LANG="en-us"> </SPAN></P>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Message: 6</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Date: Thu, 1 Sep 2011 12:06:53 -0500</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">From: Zhongyi Yuan <</FONT></SPAN><SPAN LANG="en-us"></SPAN><A HREF="mailto:zhongyi-yuan@uiowa.edu"><SPAN LANG="en-us"><U><FONT COLOR="#0000FF" FACE="Consolas">zhongyi-yuan@uiowa.edu</FONT></U></SPAN><SPAN LANG="en-us"></SPAN></A><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Subject: [Rcpp-devel] repeatable random numbers</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Hello everyone,</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">I am conducting some numerical studies that I want to be repeatable. In</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">other words, I want others who run my code to get</FONT><FONT FACE="Consolas"> exactly the same result as</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">I get.</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">This can be easily done in pure R.</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">But since I want the random number generation in Cpp, I think I need a seed</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">that can be retrieved even after re-compilation. Can this done with</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">RNGScope?</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Thanks for your help.</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Best,</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Consolas">></FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Consolas">Z</FONT><FONT FACE="Consolas">hongyi</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN></P>
</BODY>
</HTML>