[Rcpp-devel] Mersenne Twister in RcppArmadillo?

Simon Zehnder szehnder at uni-bonn.de
Sat Mar 2 22:27:46 CET 2013


Beautiful Conrad! 

This is exactly what helps to make individual random number generation easier in Armadillo/RcppArmadillo! Up to now I use solely the standard C++ RNG engine (std::mt19937) and run a loop. Now I can just use .imbue on a matrix and do not have to care about indices and index bounds. Does it work with iterators inside of .imbue? Do you think it is faster than a simple for loop? Let's wait for Dirk's experiment for the Rcpp gallery.  

Best

Simon

On Mar 2, 2013, at 4:09 PM, Conrad S <conradsand.arma at gmail.com> wrote:

> Hi everyone,
> 
> In Armadillo 3.800 I've added .imbue(), allowing a matrix to be filled
> using a functor or C++11 lambda expression. This allows the use of the
> Mersenne twister random number engine built into C++11.  For example:
> 
> std::mt19937 engine;
> std::uniform_real_distribution<double> distr(0.0, 1.0);
> 
> arma::mat X(4,5);
> X.imbue( [&]() { return distr(engine); } );
> 
> More information at:
> http://arma.sourceforge.net/docs.html#imbue
> 
> Dirk is likely to release the corresponding RcppArmadillo version soon.
> 
> 
> 
> On Fri, Feb 15, 2013 at 7:10 AM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
>> I made all my simple tests now by using Rcpp::Environment and then Rcpp::Function for "set.seed" when calling R::rnorm. The next step would be to parallelize the iterations via OpenMP. The suggestion of Yan makes quite sense to produce a RNG via std::mt19937. This is also the way I did it when I used Scythe Statistical Library.
>> As I use now the R RNG it should be able to use it in parallel, if Rcpp::Function is a kind of wrapper which contains the code of "set.seed". But I would rather guess, that it is a call object, that just calls the R function "set.seed" in R, which then would not be threadsafe (as also the RNG would then just be called when using R::rnorm).....
>> 
>> So I guess falling back to std::mt19937 is not a bad idea. For no overlapping I just increase the seed in every iteration dependent on the iteration and the number of random numbers to generate.
>> 
>> Best Simon
>> 
>> 
>> 
>> 
>> On Feb 11, 2013, at 2:16 PM, Chris Jefferson <chris at bubblescope.net> wrote:
>> 
>>> On 11/02/13 10:23, c s wrote:
>>>> On Sun, Feb 10, 2013 at 12:32 AM, Yan Zhou <zhouyan at me.com> wrote:
>>>>> 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.
>>>> The quality of the rand() function from C depends on the
>>>> implementation in libc, which varies from system to system.
>>>> 
>>>> While I'd like to incorporate a Mersenne-Twister random number
>>>> generator directly in Armadillo, it would either add a dependency on
>>>> Boost, or on C++11.  Boost might not be available on a user's system,
>>>> and the degree of support for C++11 features varies from compiler to
>>>> compiler.  We also have to bear in mind that R folks currently
>>>> disallow CRAN packages that use C++11.
>>> 
>>> If you just want a mersenne-twister random number generator, I will extract it from boost (which is under a fairly free licence).
>>> 
>>> Chris
>>> _______________________________________________
>>> 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
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list