Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
CLHEP::HepJamesRandom Class Reference

#include <JamesRandom.h>

+ Inheritance diagram for CLHEP::HepJamesRandom:

Public Member Functions

 HepJamesRandom (std::istream &is)
 
 HepJamesRandom ()
 
 HepJamesRandom (long seed)
 
 HepJamesRandom (int rowIndex, int colIndex)
 
virtual ~HepJamesRandom ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int dum=0)
 
void setSeeds (const long *seeds, int dum=0)
 
void saveStatus (const char filename[]="JamesRand.conf") const
 
void restoreStatus (const char filename[]="JamesRand.conf")
 
void showStatus () const
 
 operator unsigned int ()
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
std::string name () const
 
std::vector< unsigned long > put () const
 
bool get (const std::vector< unsigned long > &v)
 
bool getState (const std::vector< unsigned long > &v)
 
- Public Member Functions inherited from CLHEP::HepRandomEngine
 HepRandomEngine ()
 
virtual ~HepRandomEngine ()
 
bool operator== (const HepRandomEngine &engine)
 
bool operator!= (const HepRandomEngine &engine)
 
virtual double flat ()=0
 
virtual void flatArray (const int size, double *vect)=0
 
virtual void setSeed (long seed, int)=0
 
virtual void setSeeds (const long *seeds, int)=0
 
virtual void saveStatus (const char filename[]="Config.conf") const =0
 
virtual void restoreStatus (const char filename[]="Config.conf")=0
 
virtual void showStatus () const =0
 
virtual std::string name () const =0
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
virtual std::vector< unsigned long > put () const
 
virtual bool get (const std::vector< unsigned long > &v)
 
virtual bool getState (const std::vector< unsigned long > &v)
 
long getSeed () const
 
const long * getSeeds () const
 
virtual operator double ()
 
virtual operator float ()
 
virtual operator unsigned int ()
 

Static Public Member Functions

static std::string beginTag ()
 
static std::string engineName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandomEngine
static std::string beginTag ()
 
static HepRandomEnginenewEngine (std::istream &is)
 
static HepRandomEnginenewEngine (const std::vector< unsigned long > &v)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 202
 

Additional Inherited Members

- Static Protected Member Functions inherited from CLHEP::HepRandomEngine
static double exponent_bit_32 ()
 
static double mantissa_bit_12 ()
 
static double mantissa_bit_24 ()
 
static double mantissa_bit_32 ()
 
static double twoToMinus_32 ()
 
static double twoToMinus_48 ()
 
static double twoToMinus_49 ()
 
static double twoToMinus_53 ()
 
static double nearlyTwoToMinus_54 ()
 
static bool checkFile (std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
 
- Protected Attributes inherited from CLHEP::HepRandomEngine
long theSeed
 
const long * theSeeds
 

Detailed Description

Author

Definition at line 44 of file JamesRandom.h.

Constructor & Destructor Documentation

◆ HepJamesRandom() [1/4]

CLHEP::HepJamesRandom::HepJamesRandom ( std::istream &  is)

Definition at line 98 of file JamesRandom.cc.

100{
101 is >> *this;
102}

◆ HepJamesRandom() [2/4]

CLHEP::HepJamesRandom::HepJamesRandom ( )

Definition at line 66 of file JamesRandom.cc.

68{
69 long seeds[2];
70 long seed;
71
72 int cycle = std::abs(int(numEngines/maxIndex));
73 int curIndex = std::abs(int(numEngines%maxIndex));
74 ++numEngines;
75 long mask = ((cycle & 0x007fffff) << 8);
76 HepRandom::getTheTableSeeds( seeds, curIndex );
77 seed = seeds[0]^mask;
78 setSeed(seed,0);
79 setSeeds(&theSeed,0);
80}
void setSeeds(const long *seeds, int dum=0)
Definition: JamesRandom.cc:225
void setSeed(long seed, int dum=0)
Definition: JamesRandom.cc:173
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151

◆ HepJamesRandom() [3/4]

CLHEP::HepJamesRandom::HepJamesRandom ( long  seed)

Definition at line 59 of file JamesRandom.cc.

61{
62 setSeed(seed,0);
63 setSeeds(&theSeed,0);
64}

◆ HepJamesRandom() [4/4]

CLHEP::HepJamesRandom::HepJamesRandom ( int  rowIndex,
int  colIndex 
)

Definition at line 82 of file JamesRandom.cc.

84{
85 long seed;
86 long seeds[2];
87
88 int cycle = std::abs(int(rowIndex/maxIndex));
89 int row = std::abs(int(rowIndex%maxIndex));
90 int col = std::abs(int(colIndex%2));
91 long mask = ((cycle & 0x000007ff) << 20);
92 HepRandom::getTheTableSeeds( seeds, row );
93 seed = (seeds[col])^mask;
94 setSeed(seed,0);
95 setSeeds(&theSeed,0);
96}

◆ ~HepJamesRandom()

CLHEP::HepJamesRandom::~HepJamesRandom ( )
virtual

Definition at line 104 of file JamesRandom.cc.

104{}

Member Function Documentation

◆ beginTag()

std::string CLHEP::HepJamesRandom::beginTag ( )
static

Definition at line 317 of file JamesRandom.cc.

317 {
318 return "JamesRandom-begin";
319}

◆ engineName()

static std::string CLHEP::HepJamesRandom::engineName ( )
inlinestatic

Definition at line 88 of file JamesRandom.h.

88{return "HepJamesRandom";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::HepJamesRandom::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 231 of file JamesRandom.cc.

232{
233 double uni;
234
235 do {
236 uni = u[i97] - u[j97];
237 if ( uni < 0.0 ) uni++;
238 u[i97] = uni;
239
240 if (i97 == 0) i97 = 96;
241 else i97--;
242
243 if (j97 == 0) j97 = 96;
244 else j97--;
245
246 c -= cd;
247 if (c < 0.0) c += cm;
248
249 uni -= c;
250 if (uni < 0.0) uni += 1.0;
251 } while ( uni <= 0.0 || uni >= 1.0 );
252
253 return uni;
254}

Referenced by flatArray().

◆ flatArray()

void CLHEP::HepJamesRandom::flatArray ( const int  size,
double *  vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 256 of file JamesRandom.cc.

257{
258// double uni;
259 int i;
260
261 for (i=0; i<size; ++i) {
262 vect[i] = flat();
263 }
264}

◆ get() [1/2]

bool CLHEP::HepJamesRandom::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 365 of file JamesRandom.cc.

365 {
366 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
367 std::cerr <<
368 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
369 return false;
370 }
371 return getState(v);
372}
virtual std::istream & getState(std::istream &is)
Definition: JamesRandom.cc:321

◆ get() [2/2]

std::istream & CLHEP::HepJamesRandom::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 300 of file JamesRandom.cc.

300 {
301 char beginMarker [MarkerLen];
302 is >> std::ws;
303 is.width(MarkerLen); // causes the next read to the char* to be <=
304 // that many bytes, INCLUDING A TERMINATION \0
305 // (Stroustrup, section 21.3.2)
306 is >> beginMarker;
307 if (strcmp(beginMarker,"JamesRandom-begin")) {
308 is.clear(std::ios::badbit | is.rdstate());
309 std::cerr << "\nInput stream mispositioned or"
310 << "\nJamesRandom state description missing or"
311 << "\nwrong engine type found." << std::endl;
312 return is;
313 }
314 return getState(is);
315}

◆ getState() [1/2]

bool CLHEP::HepJamesRandom::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 374 of file JamesRandom.cc.

374 {
375 if (v.size() != VECTOR_STATE_SIZE ) {
376 std::cerr <<
377 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
378 return false;
379 }
380 std::vector<unsigned long> t(2);
381 for (int i=0; i<97; ++i) {
382 t[0] = v[2*i+1]; t[1] = v[2*i+2];
383 u[i] = DoubConv::longs2double(t);
384 }
385 t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
386 t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
387 t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
388 j97 = v[201];
389 i97 = (64+j97)%97;
390 return true;
391}
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114
static const unsigned int VECTOR_STATE_SIZE
Definition: JamesRandom.h:94

◆ getState() [2/2]

std::istream & CLHEP::HepJamesRandom::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 321 of file JamesRandom.cc.

321 {
322 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
323 std::vector<unsigned long> v;
324 unsigned long uu;
325 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
326 is >> uu;
327 if (!is) {
328 is.clear(std::ios::badbit | is.rdstate());
329 std::cerr << "\nJamesRandom state (vector) description improper."
330 << "\ngetState() has failed."
331 << "\nInput stream is probably mispositioned now." << std::endl;
332 return is;
333 }
334 v.push_back(uu);
335 }
336 getState(v);
337 return (is);
338 }
339
340// is >> theSeed; Removed, encompassed by possibleKeywordInput()
341
342 int ipos, jpos;
343 char endMarker [MarkerLen];
344 for (int i=0; i<97; ++i) {
345 is >> u[i];
346 }
347 is >> c; is >> cd; is >> cm;
348 is >> jpos;
349 is >> std::ws;
350 is.width(MarkerLen);
351 is >> endMarker;
352 if(strcmp(endMarker,"JamesRandom-end")) {
353 is.clear(std::ios::badbit | is.rdstate());
354 std::cerr << "\nJamesRandom state description incomplete."
355 << "\nInput stream is probably mispositioned now." << std::endl;
356 return is;
357 }
358
359 ipos = (64+jpos)%97;
360 i97 = ipos;
361 j97 = jpos;
362 return is;
363}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167

Referenced by get(), getState(), and restoreStatus().

◆ name()

std::string CLHEP::HepJamesRandom::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 51 of file JamesRandom.cc.

51{return "HepJamesRandom";}

◆ operator unsigned int()

CLHEP::HepJamesRandom::operator unsigned int ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 266 of file JamesRandom.cc.

266 {
267 return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
268 (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
269}
static double exponent_bit_32()

◆ put() [1/2]

std::vector< unsigned long > CLHEP::HepJamesRandom::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 281 of file JamesRandom.cc.

281 {
282 std::vector<unsigned long> v;
283 v.push_back (engineIDulong<HepJamesRandom>());
284 std::vector<unsigned long> t;
285 for (int i=0; i<97; ++i) {
286 t = DoubConv::dto2longs(u[i]);
287 v.push_back(t[0]); v.push_back(t[1]);
288 }
289 t = DoubConv::dto2longs(c);
290 v.push_back(t[0]); v.push_back(t[1]);
291 t = DoubConv::dto2longs(cd);
292 v.push_back(t[0]); v.push_back(t[1]);
293 t = DoubConv::dto2longs(cm);
294 v.push_back(t[0]); v.push_back(t[1]);
295 v.push_back(static_cast<unsigned long>(j97));
296 return v;
297}
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98

Referenced by put(), and saveStatus().

◆ put() [2/2]

std::ostream & CLHEP::HepJamesRandom::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 271 of file JamesRandom.cc.

271 {
272 char beginMarker[] = "JamesRandom-begin";
273 os << beginMarker << "\nUvec\n";
274 std::vector<unsigned long> v = put();
275 for (unsigned int i=0; i<v.size(); ++i) {
276 os << v[i] << "\n";
277 }
278 return os;
279}
std::vector< unsigned long > put() const
Definition: JamesRandom.cc:281

◆ restoreStatus()

void CLHEP::HepJamesRandom::restoreStatus ( const char  filename[] = "JamesRand.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 119 of file JamesRandom.cc.

120{
121 int ipos, jpos;
122 std::ifstream inFile( filename, std::ios::in);
123 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
124 std::cerr << " -- Engine state remains unchanged\n";
125 return;
126 }
127 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
128 std::vector<unsigned long> v;
129 unsigned long xin;
130 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
131 inFile >> xin;
132 if (!inFile) {
133 inFile.clear(std::ios::badbit | inFile.rdstate());
134 std::cerr << "\nJamesRandom state (vector) description improper."
135 << "\nrestoreStatus has failed."
136 << "\nInput stream is probably mispositioned now." << std::endl;
137 return;
138 }
139 v.push_back(xin);
140 }
141 getState(v);
142 return;
143 }
144
145 if (!inFile.bad() && !inFile.eof()) {
146// inFile >> theSeed; removed -- encompased by possibleKeywordInput
147 for (int i=0; i<97; ++i)
148 inFile >> u[i];
149 inFile >> c; inFile >> cd; inFile >> cm;
150 inFile >> jpos;
151 ipos = (64+jpos)%97;
152 i97 = ipos;
153 j97 = jpos;
154 }
155}
static std::string engineName()
Definition: JamesRandom.h:88
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45

◆ saveStatus()

void CLHEP::HepJamesRandom::saveStatus ( const char  filename[] = "JamesRand.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 106 of file JamesRandom.cc.

107{
108 std::ofstream outFile( filename, std::ios::out ) ;
109
110 if (!outFile.bad()) {
111 outFile << "Uvec\n";
112 std::vector<unsigned long> v = put();
113 for (unsigned int i=0; i<v.size(); ++i) {
114 outFile << v[i] << "\n";
115 }
116 }
117}

◆ setSeed()

void CLHEP::HepJamesRandom::setSeed ( long  seed,
int  dum = 0 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 173 of file JamesRandom.cc.

174{
175 // The input value for "seed" should be within the range [0,900000000]
176 //
177 // Negative seeds result in serious flaws in the randomness;
178 // seeds above 900000000 are OK because of the %177 in the expression for i,
179 // but may have the same effect as other seeds below 900000000.
180
181 int m, n;
182 float s, t;
183 long mm;
184
185 if (seed < 0) {
186 std::cout << "Seed for HepJamesRandom must be non-negative\n"
187 << "Seed value supplied was " << seed
188 << "\nUsing its absolute value instead\n";
189 seed = -seed;
190 }
191
192 long ij = seed/30082;
193 long kl = seed - 30082*ij;
194 long i = (ij/177) % 177 + 2;
195 long j = ij % 177 + 2;
196 long k = (kl/169) % 178 + 1;
197 long l = kl % 169;
198
199 theSeed = seed;
200
201 for ( n = 1 ; n < 98 ; n++ ) {
202 s = 0.0;
203 t = 0.5;
204 for ( m = 1 ; m < 25 ; m++) {
205 mm = ( ( (i*j) % 179 ) * k ) % 179;
206 i = j;
207 j = k;
208 k = mm;
209 l = ( 53 * l + 1 ) % 169;
210 if ( (l*mm % 64 ) >= 32 )
211 s += t;
212 t *= 0.5;
213 }
214 u[n-1] = s;
215 }
216 c = 362436.0 / 16777216.0;
217 cd = 7654321.0 / 16777216.0;
218 cm = 16777213.0 / 16777216.0;
219
220 i97 = 96;
221 j97 = 32;
222
223}

Referenced by HepJamesRandom(), and setSeeds().

◆ setSeeds()

void CLHEP::HepJamesRandom::setSeeds ( const long *  seeds,
int  dum = 0 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 225 of file JamesRandom.cc.

226{
227 setSeed(seeds ? *seeds : 19780503L, 0);
228 theSeeds = seeds;
229}

Referenced by HepJamesRandom().

◆ showStatus()

void CLHEP::HepJamesRandom::showStatus ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 157 of file JamesRandom.cc.

158{
159 std::cout << std::endl;
160 std::cout << "----- HepJamesRandom engine status -----" << std::endl;
161 std::cout << " Initial seed = " << theSeed << std::endl;
162 std::cout << " u[] = ";
163 for (int i=0; i<97; ++i)
164 std::cout << u[i] << " ";
165 std::cout << std::endl;
166 std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
167 << std::endl;
168 std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
169 std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
170 std::cout << "----------------------------------------" << std::endl;
171}

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::HepJamesRandom::VECTOR_STATE_SIZE = 202
static

Definition at line 94 of file JamesRandom.h.

Referenced by getState(), and restoreStatus().


The documentation for this class was generated from the following files: