Dave,<br><br>Thanks for the reply.<br>A few months ago I did not know this list yet...<br><br>I think Dirk's way of simply setting a seed in R works for me. Your way seems to be a bit complicated for me. Maybe I am missing something. Will look closer...<br>
<br>Zhongyi<br><br><div class="gmail_quote">On Thu, Sep 1, 2011 at 2:45 PM, Silkworth,David J. <span dir="ltr"><<a href="mailto:SILKWODJ@airproducts.com" target="_blank">SILKWODJ@airproducts.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<u></u>
<div>
<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" target="_blank"><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><div>
<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>
</div><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>
</div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br></blockquote></div><br>