[Rcpp-devel] [solved – use `RNGScope scp;`] R’s random number generator not initialized correctly when called at first

Paul Menzel paulepanter at users.sourceforge.net
Mon Dec 5 08:54:39 CET 2011


Am Sonntag, den 04.12.2011, 20:10 -0600 schrieb Dirk Eddelbuettel:
> On 5 December 2011 at 00:16, Paul Menzel wrote:

> | I am running an up to date Debian Sid/unstable system and I am
> | simulating how long a random walks stays negative and I am using Rcpp
> | and inline.
> | 
> |         library(inline)
> |         inc <- "
> |         #include <stdio.h>
> |         #include <stdlib.h>
> |         "
> 
> As an aside, that 'inc' snippet is not needed.

Thanks!
    
> |         rwRcpp <-cxxfunction(signature(ns="numeric", reps="numeric"),
> |         includes=inc, plugin="Rcpp", body='
> |           double n = Rcpp::as<double>(ns);
> |           double rep = Rcpp::as<double>(reps);
> |         
> |           double x = 0;
> |         
> |           Rcpp::NumericVector s(n + 2);
> |         
> |           for (int i = 0; i < rep; i++) {
> |             double len = 0;
> |             double csum = 0;
> |         
> |             do {

> Your mistake: no use of RNGScope.  Quickly google the docs, and/or the
> mailing list archives. In short, somewhere in the file add a line
> 
>   RNGScope scp;
> 
> (or some other variable name instead of scp).  Also read 'Writing R
> Extensions' on the standalone math-library and the need for keeping state.
> It simply is your error, and we plastered the header files with notes about
> how you MUSt add this.
> 
> Once added, all is well:

[…]

Indeed. Thank you very much.

> | So it looks like, Rcpp is not initializing(?) the random number
> | generator in R? Could that be the cause?
> 
> You called it incorrectly, but there is literally no way for us to prevent
> this, or else we'd put such a safeguard up.

Understood.

Thank you very much again and I am sorry for not finding that out
myself. I took the post from [1] as an example and did not think about
the `set.seed(42)` call.

Now looking for `RNGScope` it is even in section 3.3 of the `Rcpp-FAQ`
(`vignette("Rcpp-FAQ")`) and as you wrote in the header file.


Thanks,

Paul


[1] http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-September/002805.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111205/92254d4e/attachment.pgp>


More information about the Rcpp-devel mailing list