Creating a PRNG engine for in C++11 that matches PRNG results in R

前端 未结 2 342
小蘑菇
小蘑菇 2021-01-23 02:00

This question is twofold. I am translating an R script into C++ that uses the L\'Ecuyer combined multiple recursive generator (CMRG) as it\'s engine (in particular, MRG32k3a), w

2条回答
  •  无人共我
    2021-01-23 02:30

    The first question is thus, where can I find the documentation on the MRG32k3a implementation in R that specifies these parameters?

    I would use the source: https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/RNG.c#L143

    The problem is I have no idea how this is done and I can't seem to find any information anywhere (aside from knowing that engines are classes).

    The requirements for a RandomNumberEngine can be found here: https://en.cppreference.com/w/cpp/named_req/RandomNumberEngine Although it is sufficient to fulfill UniformRandomBitGenerator if you want to use uniform_real_distribution:

    Expression      Return type     Requirements
    G::result_type  T               T is an unsigned integer type
    G::min()        T               Returns the smallest value that G's operator()
                                    may return. The value is strictly less than
                                    G::max().
    G::max()        T               Returns the largest value that G's operator() may
                                    return. The value is strictly greater than
                                    G::min()
    g()             T               Returns a value in the closed interval [G::min(),
                                    G::max()]. Has amortized constant complexity.  
    

    Main problem is that MRG32k3a is meant to return a floating point number in (0,1), while a C++ UniformRandomBitGenerator returns an integer type. Why do you want to integrate with the header?

    Additional difficulties you would have to take into account:

    • Seeding strategy used by R, c.f. https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/RNG.c#L293
    • R scrambles the user supplied seed, c.f. https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/RNG.c#L272

    Alternatives would include using R source code directly without integration with the header or link to libR.

提交回复
热议问题