Help setting a constant seed or generating constant particle values

Hi Everyone,

I would like to set my simulation up such that the seed is always the same. I need to test the results from 2 detector configurations in detail (number for number) and so would like the generated particles to be identical even after re-compiling.

Is there a way to do this? I tried to set the seed so a specific value in the main but this didn’t work.


Have a look if this thread helps.

Hi Anna,

Thanks for the reply! I tried to use your solution from the thread you suggest and I have a problem linking filesystem. Because of my gcc version I used the boost library but see the following linker error :

CMakeFiles/task.dir/src/ In function PrimaryGeneratorAction::GeneratePrimaries(G4Event*)': undefined reference to boost::system::system_category()’ undefined reference to boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' CMakeFiles/task.dir/src/ In function _GLOBAL__sub_I__ZN22PrimaryGeneratorActionC2Ev’: undefined reference to boost::system::generic_category()' undefined reference to boost::system::generic_category()’ undefined reference to `boost::system::system_category()’
collect2: error: ld returned 1 exit status
make[2]: *** [task] Error 1
make[1]: *** [CMakeFiles/task.dir/all] Error 2
make: *** [all] Error 2

Code :

#include <boost/filesystem.hpp>

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
// Task 2a.2: Include the position randomization
boost::system::error_code ec;
std::string fileName = “random/event_”+std::to_string(anEvent->GetEventID())+".rndm.stat";
if (boost::filesystem::exists(fileName, ec)) {
CLHEP::HepRandom::getTheEngine()->restoreStatus (fileName.c_str());
} else {
CLHEP::HepRandom::getTheEngine()->saveStatus (fileName.c_str());

Do you have any ideas how to fix this easily?

Thanks :slight_smile:

Did you link boost libraries to your project?
But if you use boost only for that reason, there is no need, one can check otherwise if file exists, I believe this should work:

 std::ifstream file(fileName.c_str());
 if( <save> else <restore>

Yeah, of course. I think I have been staring at this so long that my mind has gone blank to anything else :smiley: Anyway your solution worked for me, thanks a lot!