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 ) {
201  return StaticRandomStates::restore(is);
202 }
203 
205 {
206  theDefaults().theEngine->showStatus();
207 }
208 
210 {
211  return static_cast<int>( theDefaults().ensureInitialized() );
212 }
213 
214 } // namespace CLHEP