21#include "CLHEP/Random/defs.h"
22#include "CLHEP/Random/MixMaxRng.h"
23#include "CLHEP/Random/Random.h"
24#include "CLHEP/Random/StaticRandomStates.h"
25#include "CLHEP/Utility/memory.h"
26#include "CLHEP/Utility/thread_local.h"
27#include "CLHEP/Utility/use_atomic.h"
33#include "CLHEP/Random/SeedTable.h"
47 :
theGenerator( &theDefaultGenerator, do_nothing_deleter() )
48 ,
theEngine ( &theDefaultEngine, do_nothing_deleter() )
51 defaults(defaults
const& other) =
delete;
52 defaults
const& operator=(defaults
const&) =
delete;
54 void resetEngine( HepRandomEngine * newEngine ) {
58 void resetEngine( HepRandomEngine & newEngine ) {
59 theEngine.reset( &newEngine, do_nothing_deleter() );
62 bool ensureInitialized() {
72 HepRandom theDefaultGenerator;
73 MixMaxRng theDefaultEngine;
82#ifdef CLHEP_USE_ATOMIC
88 class ThreadSafeDefaultsCache {
91 ThreadSafeDefaultsCache();
94 ~ThreadSafeDefaultsCache();
97 defaults* createNewDefaults();
105 DefaultsNode(DefaultsNode* iNext);
106 DefaultsNode
const* next()
const {
return next_; }
107 void setNext(DefaultsNode* v) { next_ = v; }
108 defaults* addressOfDefaults() {
return &defaults_; }
115 std::atomic<DefaultsNode*> front_;
118 ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() :
122 defaults* ThreadSafeDefaultsCache::createNewDefaults() {
123 DefaultsNode* expected = front_.load();
124 DefaultsNode* newNode =
new DefaultsNode(expected);
125 while (!front_.compare_exchange_strong(expected, newNode)) {
127 newNode->setNext(expected);
129 return newNode->addressOfDefaults();
132 ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) :
137 ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() {
138 DefaultsNode
const* node = front_.load();
140 DefaultsNode
const* next = node->next();
146 defaults & theDefaults() {
158 static ThreadSafeDefaultsCache defaultsForAllThreads;
161 static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults();
169 defaults & theDefaults() {
170 static defaults theDefaults;
180HepRandom::HepRandom()
183HepRandom::HepRandom(
long seed)
190 theDefaults().resetEngine( algorithm );
195 theDefaults().resetEngine( algorithm );
198HepRandom::~HepRandom()
201double HepRandom::flat()
203 return theDefaults().theEngine->flat();
206void HepRandom::flatArray(
const int size,
double* vect)
208 theDefaults().theEngine->flatArray(size,vect);
211double HepRandom::operator()() {
215std::string HepRandom::name()
const {
return "HepRandom";}
217 std::cerr <<
"HepRandom::engine() called -- there is no assigned engine!\n";
218 return *theDefaults().theEngine.
get();
225std::istream & operator>> (std::istream & is,
HepRandom & dist) {
229std::ostream & HepRandom::put(std::ostream & os)
const {
return os;}
230std::istream & HepRandom::get(std::istream & is) {
return is;}
236void HepRandom::setTheSeed(
long seed,
int lux)
238 theDefaults().theEngine->setSeed(seed,lux);
241long HepRandom::getTheSeed()
243 return theDefaults().theEngine->getSeed();
246void HepRandom::setTheSeeds(
const long* seeds,
int aux)
248 theDefaults().theEngine->setSeeds(seeds,aux);
251const long* HepRandom::getTheSeeds ()
253 return theDefaults().theEngine->getSeeds();
256void HepRandom::getTheTableSeeds(
long* seeds,
int index)
258 if ((index >= 0) && (index < 215)) {
259 seeds[0] = seedTable[index][0];
260 seeds[1] = seedTable[index][1];
267 return theDefaults().theGenerator.
get();
272 return theDefaults().theEngine.
get();
280void HepRandom::saveEngineStatus(
const char filename[] )
282 theDefaults().theEngine->saveStatus( filename );
285void HepRandom::restoreEngineStatus(
const char filename[] )
287 theDefaults().theEngine->restoreStatus( filename );
290std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
291 os << *getTheEngine();
295std::istream& HepRandom::restoreFullState ( std::istream & is ) {
296 is >> *getTheEngine();
300std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
301 return StaticRandomStates::save(os);
304std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
305 return StaticRandomStates::restore(is);
308void HepRandom::showEngineStatus()
310 theDefaults().theEngine->showStatus();
313int HepRandom::createInstance()
315 return static_cast<int>( theDefaults().ensureInitialized() );
std::ostream & operator<<(std::ostream &o, const Genfun::ButcherTableau &b)
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