/random/setSeeds seems to not work

Dear all,

I am using Geant4-10.5.1 in multi-threaded mode.

I am trying to initialize the random number generator so I could get different results for different runs. In the configuration file, for each run, I define
/random/setSeeds XXXXX YYYYY

In the main program I have:
G4Random::setTheEngine(new CLHEP::RanecuEngine);

but I always get the same results.

In addition, I am trying to save the generated seed values using the commands
/random/setSavingFlag 1
/random/saveEachEventFlag 1

but the expected file currentRun.rndm is not created.

Could anyone have an explanation why these commands are not working ?

Thank you!

1 Like

means the files will be named runXXXevtYYY.rndm.

In the MT mode, for each thread, there will be a new file per run, prefixed G4WorkerXXX_, plus the master file per run G4Master_ that should contain the seeds you set using /random/setSeeds command.

Thank you Anna,

But the problem is exactly that I didn’t get any runXXXevtYYY.rndm file and in addition the generated events are the same so it seems event the command
/random/setSeeds XXXXX YYYYY is not working.

I was wondering why.
The values given by /random/setSeeds XXXXX YYYYY in the configuration file are seen by G4RunMessenger.cc.

Could it be possible I need to add something else in RunAction or EventAction code?

Thank you!

This command should be sufficient to set the seeds.
Your G4 version is 10.5, correct? I found some old bug, but it got fixed in 10.0.patch03.

I’m afraid I do not know what may be happening. I would have two suggestions for you - you may try another CLHEP engine, and you may try one of the G4 examples. For instance examples/extended/eventgenerator/particleGun is using CLHEP::RanecuEngine, try adding your 3 UI commands to one of the macros there (e.g. run1.mac) and see if you get the output rndm files, and proper seeds.

Thank you Anna :-)!
Yes, I am using Geant4-10.5.1. I will try the example you suggested me.

See example TestEm4, readme item 8 : random numbers handling
May I suggest you to try to run this example with macro rndmSeed.mac
Does it work for you ? Are currentEvent.rndm and currentRun.rndm created ?

If so, check that in your application you have the equivalent of line 6 of rndmSeed.mac : /random/setSavingFlag 1

Dear Anna, Michel,

I did three tests using the following geant4 examples
Test 1: /extended/eventgenerator/particleGun
Test 2: /extended/electromagnetic/TestEm4
Test 3: basic/B5/ (my simulation is based on this example)

The command /setSavingFlag 1 works with these three examples. G4Worker*.rndm files were generated. I will compare with my code…

But the main problem I have is to generate different events for different runs by using /random/setSeeds.
This did not work even for examples TestEm4 and particleGun. I launched two runs with


/random/setSeeds 1706221035042 1706222552143


seed 2

/random/setSeeds 1555308615741 1555305948531

but the events were the same.
In fact, for /extended/eventgenerator/particleGun, in PrimaryGeneratorAction2::GeneratePrimaries(G4Event*) I printed
and the “energy” of the generated particle.

The “Current couple of seeds” and “energy” were the same for these two runs.

Do you have any idea about this? The G4UniformRand() method is used to generate this energy. Does it use the seed set in the macro file?

Thank you for your help!


I did a new test, this time with B2 example. In order to make work the command /random/setSavingFlag 1, in B2RunAction::BeginOfRunAction, I changed to « true »
the flag G4RunManager::GetRunManager()->SetRandomNumberStore(true);

I compared the generated files G4Worker… .rndm and G4Master…rndm for two different seeds set in the macro file with /random/setSeeds. The G4Worker et G4Master files are identical. At least my interpretation of these results is wrong, for different seeds we get the same results.

Does anybody have an explanation for this?
Thank you!


I did some checks and indeed, using your seeds I can confirm they are not passed correctly to the application (neither in MT, nor in sequential mode). Parser of the command /random/setSeeds converts them to integers instead of long integers, so I opened a bug report for that. That explains why both your sets of seeds result in identical events (they are always converted to the same number: INT_MAX), and why I have not seen it before (I used smaller numbers than INT_MAX).

Before it’s fixed in G4 (and for your G4 version) I believe you have 2 solutions: either you change the seeds to be below INT_MAX, or you set them directly in your application using G4Random::setTheSeeds(const long* seeds). I attach a simple messenger class that you may use to do it, just instantiate it in your main (it’s a copy of /random/setSeeds with the parsing issue fixed, renamed to /seed/setSeeds). SeedMessenger.hh (1.8 KB)

Thank you a lot for your help Anna!
Two solutions are better than one ;-)! The messenger you sent me is what I needed initially!

Just for information, waiting for an answer I found another example that seemed to be useful for me: extended/field/field04. It uses a /rndm/autoSeed command from F04RunActionMessenger.cc. This is a third possible solution.

Thank you again!!!