[Rcpp-devel] Difference between runif and unif_rand?

Dirk Eddelbuettel edd at debian.org
Sat Jul 13 08:46:13 CEST 2013


On 12 July 2013 at 21:04, Neal Fultz wrote:
| I use the debian packages. Upgrading r-cran-rcpp from testing to unstable 
| seems to have fixed this for me, so good call.

It is so easy to install from source so that I usually recommend that. And
yes, I am the maintainer of r-cran-rcpp :)

I played with this a little on the flight, and as Krzysztof noted, this was
unreproducible:


## Works here:

R> set.seed(42); table(replicate(10000, f_broke(p)))

   1    2    3    4
1029 2011 2937 4023
R> set.seed(42); table(replicate(10000, f_works(p)))

   1    2    3    4
1029 2011 2937 4023
R>

## Also:

cppFunction('
Rcpp::NumericVector unifRandTest(int n) {
  Rcpp::NumericVector x(n);
  for (int i=0; i<n; i++) {
    x[i] = unif_rand();
  }
  return x;
}
')

R> set.seed(42); a <- runif(1e5)
R> set.seed(42); b <- unifRandTest(1e 5)
R> identical(a, b)
[1] TRUE
R>


So unif_rand could be used, but I find runif (either vectorised from Rcpp
sugar or 'scalar' from the R namespace) easier as it mimicks the R use.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list