EGSnrc icon indicating copy to clipboard operation
EGSnrc copied to clipboard

Implement RANLUX random number generator in egs++

Open seirios opened this issue 3 years ago • 8 comments

This is a resubmission of https://github.com/nrc-cnrc/EGSnrc/pull/292 since my develop branch was removed. The original message was: "I noticed a lack of the RANLUX random number generator in egs++, so I implemented a class EGS_Ranlux for it based on GSL's integer implementation (gsl_rng_ranlux), which is pretty much the same as the original Fortran implementation in EGSnrc. I haven't done extensive testing, but it seems to be working fine."

seirios avatar Apr 21 '21 22:04 seirios

Thank you @serios! 🙏🏻

ftessier avatar Apr 21 '21 22:04 ftessier

Will look into the PR again. May I push to your develop branch (on EGSnrc-1) if I need to make changes?

ftessier avatar Apr 21 '21 22:04 ftessier

Sure! I opened that repo specifically for this. No problem, I'm happy to contribute to this great piece of software!

seirios avatar Apr 21 '21 22:04 seirios

@serios thanks for the invitation on the repo, but I will do it from here, i.e., push to the pull request branch you just re-created. By default, admin of the repo against which you create a PR can push to the PR branch on your repo. I was just asking if that was ok, as it might come across as a little invasive if you are not expecting it. 😄

ftessier avatar Apr 22 '21 11:04 ftessier

OK, no problem, I wasn't aware of that :)

seirios avatar Apr 22 '21 23:04 seirios

I wanted to note that C++11 added RANLUX to the standard library: https://en.cppreference.com/w/cpp/header/random (ranlux24, ranlux48). It might be worth comparing the performance of both implementations, perhaps the standard has some tricks up its sleeve.

mxxo avatar Nov 08 '21 15:11 mxxo

Thanks for the note @mxxo. This simplifies the issue of integrating GNU code inside the EGSnrc project, in terms of licensing! I am adding this to the list of projects #790.

ftessier avatar Nov 09 '21 13:11 ftessier

We ought to provide hooks in egs++ to call on the <random> generators provided as part of STL (since C++11). I don't look favorably upon maintaining our own generator code inside EGSnrc, not anymore.

In cursory testing, I found that the ranlux24 generator is ~20 times slower than the default EGSnrc ranmar. the Mersenne Twister about twice as slow as ranmar. The latter is thus a good alternative (and perhaps a good default?), while ranlux would only be practical for investigating the quality of the faster generators in a scenario where this is thought to be an issue.

For my own reference, here is a boiler plate for the C++11 STL random:

#include <random>
#include <iostream>

int main() {
    auto seed = 12345;
    std::mt19937 mt {seed};
    std::uniform_real_distribution<double> uniform(0.0, 1.0);

    for (int i=0; i<10; i++) {
        std::cout << uniform(mt) << "\n";
    }
}

I also remain curious about xoshiro and pcg64 generators to boost simulation speed.

ftessier avatar Jun 27 '22 22:06 ftessier