Access random engine from TRandomGen

Dear experts,

I would like to be able to use the same random generator instance both for TF1::GetRandom and for some builtin distributions, like Random::LogNormal.
Currently, I can do:

delete gRandom;
gRandom = new TRandomMixMax; // to be used by TF1 and TH1
ROOT::Math::RandomMixMax rmx; // to be used for rmx.LogNormal and other builtin distributions

If I use both methods at the same time to draw from different distributions, then the n-th random number from gRandom and rmx will be the same (using the default seed).
Both RandomMixMax and TRandomMixMax internally use ROOT::Math::MixMaxEngine to generate random numbers, but while RandomMixMax exposes the various builtin distributions, TRandomMixMax has a more limited choice of distributions.
In particular, the member TRandomMixMax::fEngine is not publicly accessible, and so the builtin distributions defined in ROOT::Math::MixMaxEngine are not available in gRandom.

Is there an easy way to use RandomMixMax to generate random numbers from user-defined distributions, without having to copy the code of TF1::GetRandom?
Are there plans to add a method like Engine &GetEngine() { return fEngine; } to TRandomMixMax (well, actually TRandomGen)?

Best,
Claudio


ROOT Version: 6.20
Platform: Ubuntu 16.04
Compiler: gcc


I guess @moneta can help you.

Hi,

I understand your use case. Unfortunately there is no way of getting the Engine directly from TRandomMixMax, but there is an easy workaround, given the fact that the Engine is a protected member of TRandomGen (i.e. TRandomMixMax).
The workaround is to create a derived class of TRandomGen as following:

struct Rng : public TRandomGen<ROOT::Math::MixMaxEngine<240,0>> { ROOT::Math::MixMaxEngine<240,0> & GetEngine() { return fEngine; } };

auto rng = new Rng();  
auto mixmaxEngine = rng->GetEngine();
gRandom = rng;  

I will consider for the future versions to add this possibility directly in the TRandomGen classes.
Thank you for your feedback !

Best regards

Lorenzo

Great,
thanks for the suggestion Lorenzo!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.