[Rcpp-devel] Mersenne Twister in RcppArmadillo?

Yan Zhou zhouyan at me.com
Sun Feb 10 12:24:36 CET 2013

On Feb 10, 2013, at 10:16 AM, Simon Zehnder <szehnder at uni-bonn.de> wrote:

I found a solution via Rcpp::Function and then using the R function set.seed. But I do not know what happens, when iterations run in parallel....I fear, that then this solution shows undefined behavior.
If you need to run iterations in parallel, then you need parallel RNG. To get thread-safety you can create different std::mt19937 object in different threads. Its constructor allows you to set seed. But that does not ensure the statistical property of the generated random numbers.

Yan Zhou

On Feb 10, 2013, at 10:33 AM, Simon Zehnder <szehnder at uni-bonn.de> wrote:

Actually I arrived at another fundamental question: 

I read '6.3 Random Number Generation' in the R Extensions Guide. It seems that it is not possible to set out of C++ the random seed (the wrapper RNGScope calls GetRNGstate() and PutRNGState() so it should have no extended functionality), which is very important when trying to produce reproducible results. So, I need a Mersenne-Twister, but I must be able to set the seed in every iteration. I think this brings me to the GSL library or - as long only normal random variates are needed - to the C++ std::random header?

Best Simon

On Feb 9, 2013, at 5:42 PM, Yan Zhou <zhouyan at me.com> wrote:

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.
If it is of interest to anyone, I once timed Boost, C++11 and Random123 (A high performance parallel RNG, http://www.thesalmons.org/john/random123/releases/latest/docs/, It come with a C++11 compatible RNG engine, can be used just like std::mt19937) once for different compilers on Linux. I just uploaded them to https://github.com/zhouyan/vSMC/wiki/RNG-performance-comparison

There are two benchmark, one for the performance of URNG (mt19937 etc). These include those in C++11 <random> and Boost.Random, which are almost identical in functionality (C++11 <random> is based on Boost.Random after all). Also they include two URNG from Random123, threefry and philox (both come with four basic configurations)

Another benchmark is the performance of generating distribution random numbers (such as normal). The Random123 threefry2x64 was used for all distribution and compilers, since it is the one with least performance difference between compilers.

Compilers include,
clang SVN with libstdc++ 4.7
clang SVN with libc++ SVN
gcc 4.7
intel icpc 13

clang and gcc version also come with results when using AMD libm instead of glibc. However, the benchmark are not Rcpp specific. They are compiled to standalone C++ programs. But all these URNGs can be used Rcpp. As demonstrated in Dirk's example. 


Yan Zhou

Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130210/5d7ae4fb/attachment.html>

More information about the Rcpp-devel mailing list