45 :
theGenerator( &theDefaultGenerator, do_nothing_deleter() )
46 ,
theEngine ( &theDefaultEngine, do_nothing_deleter() )
49 defaults(defaults
const& other) =
delete;
50 defaults
const&
operator=(defaults
const&) =
delete;
52 void resetEngine( HepRandomEngine * newEngine ) {
56 void resetEngine( HepRandomEngine & newEngine ) {
57 theEngine.reset( &newEngine, do_nothing_deleter() );
60 bool ensureInitialized() {
70 HepRandom theDefaultGenerator;
71 MixMaxRng theDefaultEngine;
80#ifdef CLHEP_USE_ATOMIC
86 class ThreadSafeDefaultsCache {
89 ThreadSafeDefaultsCache();
92 ~ThreadSafeDefaultsCache();
95 defaults* createNewDefaults();
103 DefaultsNode(DefaultsNode* iNext);
104 DefaultsNode
const* next()
const {
return next_; }
105 void setNext(DefaultsNode* v) { next_ = v; }
106 defaults* addressOfDefaults() {
return &defaults_; }
113 std::atomic<DefaultsNode*> front_;
116 ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() :
120 defaults* ThreadSafeDefaultsCache::createNewDefaults() {
121 DefaultsNode* expected = front_.load();
122 DefaultsNode* newNode =
new DefaultsNode(expected);
123 while (!front_.compare_exchange_strong(expected, newNode)) {
125 newNode->setNext(expected);
127 return newNode->addressOfDefaults();
130 ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) :
135 ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() {
136 DefaultsNode
const* node = front_.load();
138 DefaultsNode
const* next = node->next();
144 defaults & theDefaults() {
156 static ThreadSafeDefaultsCache defaultsForAllThreads;
159 static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults();
167 defaults & theDefaults() {
168 static defaults theDefaults;
178HepRandom::HepRandom()
181HepRandom::HepRandom(
long seed)
188 theDefaults().resetEngine( algorithm );
193 theDefaults().resetEngine( algorithm );
196HepRandom::~HepRandom()
199double HepRandom::flat()
201 return theDefaults().theEngine->flat();
204void HepRandom::flatArray(
const int size,
double* vect)
206 theDefaults().theEngine->flatArray(size,vect);
209double HepRandom::operator()() {
213std::string HepRandom::name()
const {
return "HepRandom";}
215 std::cerr <<
"HepRandom::engine() called -- there is no assigned engine!\n";
216 return *theDefaults().theEngine.
get();
227std::ostream & HepRandom::put(std::ostream & os)
const {
return os;}
228std::istream & HepRandom::get(std::istream & is) {
return is;}
234void HepRandom::setTheSeed(
long seed,
int lux)
236 theDefaults().theEngine->setSeed(seed,lux);
239long HepRandom::getTheSeed()
241 return theDefaults().theEngine->getSeed();
244void HepRandom::setTheSeeds(
const long* seeds,
int aux)
246 theDefaults().theEngine->setSeeds(seeds,aux);
249const long* HepRandom::getTheSeeds ()
251 return theDefaults().theEngine->getSeeds();
254void HepRandom::getTheTableSeeds(
long* seeds,
int index)
256 if ((index >= 0) && (index < 215)) {
257 seeds[0] = seedTable[index][0];
258 seeds[1] = seedTable[index][1];
265 return theDefaults().theGenerator.
get();
270 return theDefaults().theEngine.
get();
278void HepRandom::saveEngineStatus(
const char filename[] )
280 theDefaults().theEngine->saveStatus( filename );
283void HepRandom::restoreEngineStatus(
const char filename[] )
285 theDefaults().theEngine->restoreStatus( filename );
288std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
289 os << *getTheEngine();
293std::istream& HepRandom::restoreFullState ( std::istream & is ) {
294 is >> *getTheEngine();
298std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
299 return StaticRandomStates::save(os);
302std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
303 return StaticRandomStates::restore(is);
306void HepRandom::showEngineStatus()
308 theDefaults().theEngine->showStatus();
311int HepRandom::createInstance()
313 return static_cast<int>( theDefaults().ensureInitialized() );
std::istream & operator>>(std::istream &s, G4BetaDecayType &q)
std::ostream & operator<<(std::ostream &out, const G4CellScoreComposer &ps)
std::shared_ptr< HepRandom > theGenerator
std::shared_ptr< HepRandomEngine > theEngine
virtual std::istream & get(std::istream &is)
virtual std::istream & get(std::istream &is)
virtual std::ostream & put(std::ostream &os) const
yystype & operator=(const yystype &right)
#define CLHEP_THREAD_LOCAL