[Rcpp-devel] repeatable random numbers

Dirk Eddelbuettel edd at debian.org
Thu Sep 1 21:35:25 CEST 2011


On 1 September 2011 at 14:23, Zhongyi Yuan wrote:
| If there is a seed set in R before rngs() is called, then "RNGScope z;" can be
| omitted, right?

Don't think about it this way. Think about properly initializing the state of
the RNG, and instantiating a local RNGScope variable does that. Which is why
we strongly recommend that usage pattern.
 
| I used to use RNGScope() to set seed. I guess that is not the right way, is it?
| 
| My test shows, with RNGScope(), even if I re-compile (without quitting R), rngs
| always gives the same numbers. I have to quit R and then re-compile to get
| random numbers. There is no such problem with "RNGScope z".

I don't quite understand what you are trying to say here.  To get different
numbers, you need different seeds. Initializing based on the clock (and/or
process id) is a common trick to ensure this.

Don't get me wrong but this now seems to have little to do with Rcpp; you may
want to read up on random numbers, simulations and do some experiments with
plain C or C++ code. 

Please do not abuse this list as a substitute for doing your independent
study and reading.

Dirk 

 
| Zhongyi
| 
| On Thu, Sep 1, 2011 at 12:15 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
| 
|     I should have written that quick function as follows:
| 
|       rngs <- cxxfunction(signature(), plugin="Rcpp",
|                           body="RNGScope z; return Rcpp::rnorm
|     (5,0,1);")
| 
|     You _do_ want RNGScope around; my bad for omitting it in the previous post.
| 
|     Dirk
| 
|     --
|     Two new Rcpp master classes for R and C++ integration scheduled for
|     New York (Sep 24) and San Francisco (Oct 8), more details are at
|     http://dirk.eddelbuettel.com/blog/2011/08/04#
|     rcpp_classes_2011-09_and_2011-10
|     http://www.revolutionanalytics.com/products/training/public/
|     rcpp-master-class.php
| 
| 

-- 
Two new Rcpp master classes for R and C++ integration scheduled for 
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php


More information about the Rcpp-devel mailing list