CLHEP::Ranlux64Engine issues, with multi-threading

So, I’m not sure whether one would consider this a bug with Geant4, or a bug with CLHEP. I can’t figure out which forum or sub-forum this post belongs on. Maybe it’s not a bug at all and I’m simply doing something very very wrong.

So, I’m running geant4-10-05-patch-01 in multi-threaded mode, and I decided to use the CLHEP::Ranlux64Engine engine to generate a flat distribution. I noticed that the results were coming out very biased. Sometimes.

In an effort to get this down to a minimal example, I took Example B1, commented out the place (in main() ) where it calls “G4Random::setTheEngine(new CLHEP::RanecuEngine);”, replaced it with the Ranlux64Engine, and added in the following chunk of code in the B1EventAction::BeginOfEventAction(const G4Event*) function:

for(int i=0; i<20; i++)
double costheta_lab;
costheta_lab = G4RandFlat::shoot(-1.0, 1.0);
cout << costheta_lab << ", ";
cout << endl;

The 9th printed random number in every ‘event’ is almost always between -0.90 and -1.0. This seems to only happen when one “edge” of the flat distribution is negative.

It also only happens within B1EventAction::BeginOfEventAction(). If I put the same chunk of code in main(), it comes out fine. This is, incidentally, what makes me suspect that it’s related to multi-threading.

So, obviously that’s not the sort of behavior one wants from a random number generator. Is this a known issue? (I can’t find anyone else complaining about it.) Have I done something foolish here? Should I just pick another random number generator engine and be done with it?

Hi, this behaviour is likely specific to Ranlux64Engine, meaning that the way it gets seeded is not the most appropriate. Seeding of random engines is a complex matter and highly depends on features of the engine itself. I remind you that in MT mode, Geant4 provides for convenience a default seeding mechanism, which in any case cannot guarantee un-correlation of seeds; the choice of an appropriate seeding strategy is the user’s responsibility. You can take a look in the Geant4 Toolkit Developers Guide for how to control seeding in MT runs:

The other suggestion I can give is to simply comment out the choice of the random engine in the main() and therefore implicitly use the default engine, which is now MixMax, known to provide a much more robust internal seeding in 64 bits.


Yes – as you suggest, the issue seems to not show up with the MixMaxRng engine. I didn’t realize this was the recommended random engine these days for multithreading, so I’m happy enough to just use that one and call it good.