CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Random.cc
Go to the documentation of this file.
1 // $Id: Random.cc,v 1.6 2010/06/16 17:24:53 garren Exp $
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- HepRandom ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 
11 // =======================================================================
12 // Gabriele Cosmo - Created: 5th September 1995
13 // - Minor corrections: 31st October 1996
14 // - Added methods for engine status: 19th November 1996
15 // - HepRandom defined as singleton, constructors are
16 // kept public for backward compatibility: 27th Feb 1998
17 // - Relocated Poisson and Gauss data and simplified
18 // initialisation of static generator: 5th Jan 1999
19 // =======================================================================
20 
21 #include "CLHEP/Random/defs.h"
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 
27 // -----------------------------
28 // Static members initialisation
29 // -----------------------------
30 
31 #include "CLHEP/Random/SeedTable.h"
32 
33 namespace CLHEP {
34 
35 
36 namespace {
37 
38 struct defaults {
39  defaults( HepRandom & g, HepJamesRandom & e )
40  : theGenerator( &g, do_nothing_deleter() )
41  , theEngine ( &e, do_nothing_deleter() )
42  { }
43 
44  void resetEngine( HepRandomEngine * newEngine ) {
45  theEngine.reset( newEngine );
46  }
47 
48  void resetEngine( HepRandomEngine & newEngine ) {
49  theEngine.reset( &newEngine, do_nothing_deleter() );
50  }
51 
52  bool ensureInitialized() {
53  assert( theGenerator.get() != 0 && theEngine.get() != 0 );
54  return true;
55  }
56 
57  ~defaults()
58  { }
59 
60  shared_ptr<HepRandom > theGenerator;
61  shared_ptr<HepRandomEngine> theEngine;
62 }; // defaults
63 
64  inline
65  defaults & theDefaults() {
66  static HepRandom theDefaultGenerator;
67  static HepJamesRandom theDefaultEngine;
68  static defaults theDefaults(theDefaultGenerator, theDefaultEngine);
69  return theDefaults;
70  }
71 
72 } // namespace
73 
74 //---------------------------- HepRandom ---------------------------------
75 
77 { }
78 
80 {
81  setTheSeed(seed);
82 }
83 
85 {
86  theDefaults().resetEngine( algorithm );
87 }
88 
90 {
91  theDefaults().resetEngine( algorithm );
92 }
93 
95 { }
96 
98 {
99  return theDefaults().theEngine->flat();
100 }
101 
102 void HepRandom::flatArray(const int size, double* vect)
103 {
104  theDefaults().theEngine->flatArray(size,vect);
105 }
106 
108  return flat();
109 }
110 
111 std::string HepRandom::name() const {return "HepRandom";}
113  std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
114  return *theDefaults().theEngine.get();
115 }
116 
117 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
118  return dist.put(os);
119 }
120 
121 std::istream & operator>> (std::istream & is, HepRandom & dist) {
122  return dist.get(is);
123 }
124 
125 std::ostream & HepRandom::put(std::ostream & os) const {return os;}
126 std::istream & HepRandom::get(std::istream & is) {return is;}
127 
128 // --------------------------
129 // Static methods definitions
130 // --------------------------
131 
132 void HepRandom::setTheSeed(long seed, int lux)
133 {
134  theDefaults().theEngine->setSeed(seed,lux);
135 }
136 
138 {
139  return theDefaults().theEngine->getSeed();
140 }
141 
142 void HepRandom::setTheSeeds(const long* seeds, int aux)
143 {
144  theDefaults().theEngine->setSeeds(seeds,aux);
145 }
146 
148 {
149  return theDefaults().theEngine->getSeeds();
150 }
151 
152 void HepRandom::getTheTableSeeds(long* seeds, int index)
153 {
154  if ((index >= 0) && (index < 215)) {
155  seeds[0] = seedTable[index][0];
156  seeds[1] = seedTable[index][1];
157  }
158  else seeds = NULL;
159 }
160 
162 {
163  return theDefaults().theGenerator.get();
164 }
165 
167 {
168  return theDefaults().theEngine.get();
169 }
170 
172 {
173  theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
174 }
175 
176 void HepRandom::saveEngineStatus( const char filename[] )
177 {
178  theDefaults().theEngine->saveStatus( filename );
179 }
180 
181 void HepRandom::restoreEngineStatus( const char filename[] )
182 {
183  theDefaults().theEngine->restoreStatus( filename );
184 }
185 
186 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
187  os << *getTheEngine();
188  return os;
189 }
190 
191 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
192  is >> *getTheEngine();
193  return is;
194 }
195 
196 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
197  return StaticRandomStates::save(os);
198 }
199 
200 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
202 }
203 
205 {
206  theDefaults().theEngine->showStatus();
207 }
208 
210 {
211  return static_cast<int>( theDefaults().ensureInitialized() );
212 }
213 
214 } // namespace CLHEP
CLHEP::HepRandom::seedTable
static const long seedTable[215][2]
Definition: Matrix/CLHEP/Random/Random.h:158
CLHEP::HepRandom::saveFullState
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:186
CLHEP::HepRandomEngine
Definition: Matrix/CLHEP/Random/RandomEngine.h:55
theGenerator
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:60
g
int g(shared_ptr< X >)
Definition: testSharedPtrConvertible.cc:46
CLHEP::HepRandom::createInstance
static int createInstance()
Definition: Random.cc:209
theEngine
shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:61
CLHEP::HepRandom::restoreStaticRandomStates
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition: Random.cc:200
CLHEP::HepRandom::getTheTableSeeds
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:152
is
HepRotation and so forth isNear() norm2() rectify() static Rotation row1 row4(To avoid bloat in the code pulled in for programs which don 't use all these features, we split the implementation .cc files. Only isNear() goes into the original Rotation.cc) --------------------------------------- HepAxisAngle and HepEulerAngles classes --------------------------------------- These classes are very useful and simple structures for holding the result of a nice intuituve decomposition of a rotation there is no longer much content in the distinct ZOOM PhysicsVectors library The only content left in the library is the object files representing the various Exception objects When we build the CLHEP classes for the ZOOM we will set up so as to use ZOOM SpaceVector is(but we can disable namespace usage and most of our users do so at this point). What I do is leave Hep3Vector in the global namespace
CLHEP::HepRandom::restoreFullState
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:191
CLHEP::HepRandom::setTheEngine
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:171
CLHEP::StaticRandomStates::restore
static std::istream & restore(std::istream &is)
Definition: StaticRandomStates.cc:51
CLHEP::HepRandom::restoreEngineStatus
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:181
CLHEP::HepRandom::getTheSeeds
static const long * getTheSeeds()
Definition: Random.cc:147
CLHEP::HepRandomEngine::get
virtual std::istream & get(std::istream &is)
Definition: RandomEngine.cc:62
size
user code seldom needs to call this function directly ZMerrno whether or not they are still recorded ZMerrno size() Return the(integer) number of ZMthrow 'n exceptions currently recorded. 5) ZMerrno.clear() Set an internal counter to zero. This counter is available(see next function) to user code to track ZMthrow 'n exceptions that have occurred during any arbitrary time interval. 6) ZMerrno.countSinceCleared() Return the(integer) number of ZMthrow 'n exceptions that have been recorded via ZMerrno.write()
CLHEP::HepRandom::operator()
virtual double operator()()
Definition: Random.cc:107
CLHEP::HepRandom::getTheGenerator
static HepRandom * getTheGenerator()
Definition: Random.cc:161
CLHEP::HepRandom::engine
virtual HepRandomEngine & engine()
Definition: Random.cc:112
CLHEP
Definition: ClhepVersion.h:13
CLHEP::do_nothing_deleter
Definition: Matrix/CLHEP/Utility/memory.h:1477
CLHEP::HepRandom::name
virtual std::string name() const
Definition: Random.cc:111
CLHEP::HepRandom::get
virtual std::istream & get(std::istream &is)
Definition: Random.cc:126
CLHEP::HepRandom::getTheSeed
static long getTheSeed()
Definition: Random.cc:137
algorithm
I could not create a faster method completely accurate that does not require overly large tables and takes a major step up when we cross for several values of but we have applied this with much higher N We validated the main trials It showed no sign of approaching the rejectable p values or errors in mean and sigma the method matches the original algorithm
Definition: validation.doc:308
CLHEP::HepRandom::setTheSeeds
static void setTheSeeds(const long *seeds, int aux=-1)
Definition: Random.cc:142
CLHEP::StaticRandomStates::save
static std::ostream & save(std::ostream &os)
Definition: StaticRandomStates.cc:37
CLHEP::operator>>
std::istream & operator>>(std::istream &is, HepAxisAngle &aa)
Definition: AxisAngle.cc:96
seeds
Technical Maintenance Note for CLHEP Random Consequences of seeding JamesRandom with positive seed values greater than In the source code JamesRandom The usual way of seeding a generator is via the default which makes use of the table of seeds(with some trickery to ensure that the values won 't repeat after the table rows are exhausted). The trickery preserves the fact that sees are never negative(because the table values are never negative
CLHEP::HepRandom::saveStaticRandomStates
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition: Random.cc:196
CLHEP::HepRandom::flatArray
void flatArray(const int size, double *vect)
Definition: Random.cc:102
CLHEP::HepRandom::saveEngineStatus
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:176
CLHEP::HepRandom::showEngineStatus
static void showEngineStatus()
Definition: Random.cc:204
CLHEP::HepRandom::setTheSeed
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:132
CLHEP::operator<<
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition: AxisAngle.cc:86
CLHEP::HepRandom::getTheEngine
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
CLHEP::HepRandom::flat
double flat()
Definition: Random.cc:97
CLHEP::HepRandom::put
virtual std::ostream & put(std::ostream &os) const
Definition: Random.cc:125
CLHEP::HepRandom
Definition: Matrix/CLHEP/Random/Random.h:50
CLHEP::HepRandom::HepRandom
HepRandom()
Definition: Random.cc:76
CLHEP::HepRandom::~HepRandom
virtual ~HepRandom()
Definition: Random.cc:94