[Rcpp-devel] Mersenne Twister in RcppArmadillo?
edd at debian.org
Sat Feb 9 16:46:04 CET 2013
On 9 February 2013 at 14:32, Yan Zhou wrote:
| You have at least two choices.
| 1. Just use std::mt19937 as you do in other C++ code.
| std::mt19937 eng;
| std::normal_distribuiton rnorm(0, 10); // or any other dist you need
| arma::vec v(N);
| for (int i = 0; i != N; ++i)
| v[i] = rnorm(eng);
| 2. Rcpp has convenient way to use R runtime's RNG, which I believe by default is Mersenne Twister. See doc of Rcpp sugar. Also you can access Rmath directly.
Yep. Via R::rnorm as scalar and Rcpp::rnorm via sugar as a vector.
| To have Armadillo randn use MT19937 is not easy. Since it use srand() for seed, I guess it also use C rand(), whose quality is quite questionable.
This really is a BIG topic and worth a few more comments. Note that I wrote a
few related posts on RNGs at the Rcpp Gallery, see for example
which compares the RNGs from R, C++11 and Boost. Simon just added Armadillo to
the list, we can add even more RNGs fromn other packages.
I like using R's own, eg Rcpp::rnorm() which is already vectorised as it
gives you _exactly_ the same stream as you get from R (if you set the seed
the same, obviously).
And to answer the question, yes, that uses the Mersenne Twister to. So you
can create an RcppVector full of N(0,1) (or scaled / moved) and instantiate
an Armadillo vector just as easily.
So if someone wants to experiment more: go ahead, and please contribute novel
approaches to the Rcpp Galltery.
| Yan Zhou
| On Feb 9, 2013, at 2:24 PM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
| > Dear Rcpp-Devels,
| > I am right now working on a simulation where I use the 'randn()' function of Armadillo. I know, that the seed can be controlled via std::srand(). What I would like to know is: What kind of Generator is used by Armadillo? Can I in some way use the std::mt19937 or std::mt19937_64 random generator from the C++ random header? Actually this object is an instantiation of the mersenne_twister_engine, which is then for instance passed to the C++ 'shuffle()' function from the algorithm header. How to make Armadillo aware of the mersenne_twister_engine is something I haven't understood yet.
| > Has anyone experience with this?
| > Best
| > Simon
| > _______________________________________________
| > Rcpp-devel mailing list
| > Rcpp-devel at lists.r-forge.r-project.org
| > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel