Different random seeds, but same results

Dear all,

I am trying run a simulation in multi thread with automatically updating random seed with systime so that I can run it multiple times with same input and not to get a duplicated results. So I put this in my main.cc:

G4Random::setTheEngine(new CLHEP::RanecuEngine());
G4long seed = time(NULL);
G4Random::setTheSeed(seed);

and also in my RunAction.cc:

long seeds[2];
time_t systime = time(NULL);
seeds[0] = (long) systime;
seeds[1] = (long) (systime*G4UniformRand());
G4Random::setTheSeeds(seeds);

and I checked with
G4cout << "Seed: " << G4Random::getTheSeed() << G4endl;

to make sure that I have a different seeds every time.

However, when I run a compiled executable with a macro, and then I turn off the executable and rerun the executable with same macro again, it gives me the same result with previous run, even though the seed is shown differently.

Am I missing something? I am very new to programming, so I might have missed something should have been done.

Any help is greatly appreciated. Thanks.

2 Likes

This is discussed in the Geant4 documentation, but not in the Application Developers Guide. The Toolkit Developers Guide, which is generally aimed at G4 collaborators, has a whole section on multithreading, including guidance on how to initialize the random engine:

http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/ForToolkitDeveloper/html/OOAnalysisDesign/Multithreading/mt.html?highlight=seed#random-number-generation-seeding-in-mt

2 Likes

Thank you very much for pointing out.

It seems like that I have to have a function like MyRunManager::InitializeSeeds and place it inside the BeginoOfRunaction of my RunAction.cc?

And to do so it requires number of events and number of seeds per event.
I could get number of events using NumberOfEventsToBeProcessed(), but how do I get the number of seeds per event in BeginOfRunAction?

I appreciate it in advance.

Hi Sblim,

When I run the macro in the Batch mode I get different results as expected with just the simple addition of the following lines in the main.cc :

CLHEP::HepRandom::setTheSeed(seed); G4Random::setTheSeed(seed);

The seed is also a long generated from the beginning time of the simulation.

Did you try it in batch mode and got problems as well?

In case this is of help: the seed needs to be set before G4MTRunManager is instantiated. G4MTRunManager contains a master random number generator which is used to generate the seeds for the events. This generator is cloned from G4Random::getTheEngine() when G4MTRunManager is instantiated.

This fixes the issue, without any need for overriding InitializeSeeds. I hope this helps!

3 Likes