22 #include "CLHEP/Random/JamesRandom.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" 
   46       defaults(defaults 
const& other) = 
delete;
 
   47       defaults 
const& operator=(defaults 
const&) = 
delete;
 
   49       void  resetEngine( HepRandomEngine * newEngine ) {
 
   53       void  resetEngine( HepRandomEngine & newEngine ) {
 
   54         theEngine.reset( &newEngine, do_nothing_deleter() );
 
   57       bool  ensureInitialized()  {
 
   77 #ifdef CLHEP_USE_ATOMIC 
   83     class ThreadSafeDefaultsCache {
 
   86       ThreadSafeDefaultsCache();
 
   89       ~ThreadSafeDefaultsCache();
 
   92       defaults* createNewDefaults();
 
  100         DefaultsNode(DefaultsNode* iNext);
 
  101         DefaultsNode 
const* next()
 const { 
return next_; }
 
  102         void setNext(DefaultsNode* v) { next_ = v; }
 
  103         defaults* addressOfDefaults() { 
return &defaults_; }
 
  110       std::atomic<DefaultsNode*> front_;
 
  113     ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() :
 
  117     defaults* ThreadSafeDefaultsCache::createNewDefaults() {
 
  118       DefaultsNode* expected = front_.load();
 
  119       DefaultsNode* newNode = 
new DefaultsNode(expected);
 
  120       while (!front_.compare_exchange_strong(expected, newNode)) {
 
  122         newNode->setNext(expected);
 
  124       return newNode->addressOfDefaults();
 
  127     ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) :
 
  132     ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() {
 
  133       DefaultsNode 
const* node = front_.load();
 
  135         DefaultsNode 
const* next = node->next();
 
  141     defaults &  theDefaults()  {
 
  153       static ThreadSafeDefaultsCache defaultsForAllThreads;
 
  156       static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults();
 
  164     defaults &  theDefaults()  {
 
  165       static defaults theDefaults;
 
  175 HepRandom::HepRandom()
 
  178 HepRandom::HepRandom(
long seed)
 
  183 HepRandom::HepRandom(HepRandomEngine & algorithm)
 
  185   theDefaults().resetEngine( algorithm );
 
  188 HepRandom::HepRandom(HepRandomEngine * algorithm)
 
  190   theDefaults().resetEngine( algorithm );
 
  193 HepRandom::~HepRandom()
 
  198   return theDefaults().theEngine->flat();
 
  201 void HepRandom::flatArray(
const int size, 
double* vect)
 
  203   theDefaults().theEngine->flatArray(size,vect);
 
  206 double HepRandom::operator()() {
 
  212   std::cerr << 
"HepRandom::engine() called -- there is no assigned engine!\n";
 
  213   return *theDefaults().theEngine.get();
 
  216 std::ostream & 
operator<< (std::ostream & os, 
const HepRandom & dist) {
 
  220 std::istream & 
operator>> (std::istream & is, HepRandom & dist) {
 
  224 std::ostream & HepRandom::put(std::ostream & os)
 const {
return os;}
 
  225 std::istream & HepRandom::get(std::istream & is) {
return is;}
 
  231 void HepRandom::setTheSeed(
long seed, 
int lux)
 
  233   theDefaults().theEngine->setSeed(seed,lux);
 
  236 long HepRandom::getTheSeed()
 
  238   return theDefaults().theEngine->getSeed();
 
  241 void HepRandom::setTheSeeds(
const long* seeds, 
int aux)
 
  243   theDefaults().theEngine->setSeeds(seeds,aux);
 
  246 const long* HepRandom::getTheSeeds ()
 
  248   return theDefaults().theEngine->getSeeds();
 
  251 void HepRandom::getTheTableSeeds(
long* seeds, 
int index)
 
  253   if ((index >= 0) && (index < 215)) {
 
  254     seeds[0] = seedTable[index][0];
 
  255     seeds[1] = seedTable[index][1];
 
  260 HepRandom * HepRandom::getTheGenerator()
 
  262   return theDefaults().theGenerator.get();
 
  265 HepRandomEngine * HepRandom::getTheEngine()
 
  267   return theDefaults().theEngine.get();
 
  270 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine)
 
  272   theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
 
  275 void HepRandom::saveEngineStatus( 
const char filename[] )
 
  277   theDefaults().theEngine->saveStatus( filename );
 
  280 void HepRandom::restoreEngineStatus( 
const char filename[] )
 
  282   theDefaults().theEngine->restoreStatus( filename );
 
  285 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
 
  286   os << *getTheEngine();
 
  290 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
 
  291   is >> *getTheEngine();
 
  295 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
 
  296   return StaticRandomStates::save(os);
 
  299 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
 
  300   return StaticRandomStates::restore(is);
 
  303 void HepRandom::showEngineStatus()
 
  305   theDefaults().theEngine->showStatus();
 
  308 int HepRandom::createInstance()
 
  310   return static_cast<int>( theDefaults().ensureInitialized() );
 
std::ostream & operator<<(std::ostream &os, const HepRandom &dist)
 
std::shared_ptr< HepRandom > theGenerator
 
std::shared_ptr< HepRandomEngine > theEngine
 
static constexpr double lux
 
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
 
const char * name(G4int ptype)
 
std::istream & operator>>(std::istream &is, HepRandom &dist)
 
HepJamesRandom theDefaultEngine
 
HepRandom theDefaultGenerator