Inconsistency when restoring Ranecu engine status from file

In my application I see an apparent inconsistency in the random engine after loading the status from a file. I load the status for the Ranecu engine from this file:

Uvec
1878463799
2
99775361
86969112

using some code that I summarized in this reproducer:

#include <CLHEP/Random/Randomize.h>

int main(){

  // Create the engine and set the initial seeds
  CLHEP::RanecuEngine rndmEngine;
  CLHEP::HepRandom::setTheEngine(&rndmEngine);
  long seeds[2] = {1, 2};
  CLHEP::HepRandom::setTheSeeds(seeds, 0);

  // Show the status and the seeds
  CLHEP::HepRandom::showEngineStatus();
  std::cout << "Seeds: " << CLHEP::HepRandom::getTheSeeds()[0] << " " <<  CLHEP::HepRandom::getTheSeeds()[1] << std::endl;

  // Load the status from file
  CLHEP::HepRandom::restoreEngineStatus("engineStatus.txt");

  // Show the status and the seeds again
  CLHEP::HepRandom::showEngineStatus();
  std::cout << "Seeds: " << CLHEP::HepRandom::getTheSeeds()[0] << " " <<  CLHEP::HepRandom::getTheSeeds()[1] << std::endl;
}

Compiling and launching the reproducer I get:

--------- Ranecu engine status ---------
 Initial seed (index) = 0
 Current couple of seeds = 1, 2
----------------------------------------
Seeds: 1 2

--------- Ranecu engine status ---------
 Initial seed (index) = 2
 Current couple of seeds = 99775361, 86969112
----------------------------------------
Seeds: 1 2

So it seems that the seeds reported by CLHEP::HepRandom::showEngineStatus() and the ones returned by CLHEP::HepRandom::getTheSeeds() are different after setting the engine status from file. The first method seems to indicate that the status has been correctly loaded from file, but then the second one gives different seed values so I’m not sure. Is there something I don’t understand or is this maybe a bug?
Thanks in advance for any help.

Using the CLHEP version bundled with Geant4 10.06.p02.

I checked the Ranecu engine code in CLHEP and I think I found something. In RanecuEngine::getState the seeds read from the file are inserted in the seeds table at the position given by the index read from file as well; in the case reported at the begin of the initial post of this thread this index is 2. But then the theSeeds member variable is not reset to point at the seeds in the given table entry (2) but keeps pointing at the old entry (0). Thus when calling G4Random::getTheSeeds() the seeds at entry 0 are returned, even if internally the engine is using the ones at entry 2.

I don’t understand if one is not supposed to change the seed index of an engine (I guess so because messing with this can result in two engines using the same seeds) but then I don’t understand why theSeed is updated with the value read from file. By the way, if I’m correct this also prevents to reproduce a multi-threaded run reading the seeds from file event by event with a different number of threads.

No, you misunderstand. The seeds you’re printing are your -initial- seeds you’ve set at the beginning of the job. For Ranecu (and only for Ranecu) a table of seeds is used and those seeds stored in the table won’t change. The status of the engine is what matters and allows you to reproduce the exact situation at the time you saved it.
btw, I suggest you to move away from the obsolete Ranecu and rather use the default engine MixMax which is now available, fully supporting 64 bits architectures.

1 Like

Thanks for the hint, I’ll move to MixMax.