Thank you for the quick reply, Dirk.<br><br>Zhongyi<br><br><div class="gmail_quote">On Thu, Sep 1, 2011 at 12:13 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</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;"><div><div></div><div class="h5"><br>
On 1 September 2011 at 12:06, Zhongyi Yuan wrote:<br>
| Hello everyone,<br>
|<br>
| I am conducting some numerical studies that I want to be repeatable. In other<br>
| words, I want others who run my code to get exactly the same result as I get.<br>
| This can be easily done in pure R.<br>
|<br>
| But since I want the random number generation in Cpp, I think I need a seed<br>
| that can be retrieved even after re-compilation. Can this done with RNGScope?<br>
<br>
</div></div>Yes you can set a seed the same way, see 'Writing R Extensions'. We hook<br>
into the same C API for R's RNG as ... R itself does.<br>
<br>
Trivial example:<br>
<br>
<br>
R> require(inline)<br>
Loading required package: inline<br>
R> rngs <- cxxfunction(signature(), plugin="Rcpp", body="return Rcpp::rnorm(5,0,1);")<br>
R> set.seed(42)<br>
R> rngs()<br>
[1] 1.370958 -0.564698 0.363128 0.632863 0.404268<br>
R> rngs()<br>
[1] -0.1061245 1.5115220 -0.0946590 2.0184237 -0.0627141<br>
R> set.seed(42)<br>
R> rnorm(5)<br>
[1] 1.370958 -0.564698 0.363128 0.632863 0.404268<br>
R> rnorm(5)<br>
[1] -0.1061245 1.5115220 -0.0946590 2.0184237 -0.0627141<br>
R><br>
<br>
So both draws get you the _exact same numbers_ as we reset the RNG state to<br>
the same starting point.<br>
<br>
Dirk<br>
<div class="im"><br>
| Thanks for your help.<br>
|<br>
| Best,<br>
| Zhongyi<br>
| <br>
|<br>
</div>| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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>
<font color="#888888"><br>
--<br>
Two new Rcpp master classes for R and C++ integration scheduled for<br>
New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
<a href="http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10</a><br>
<a href="http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php" target="_blank">http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php</a><br>
</font></blockquote></div><br>