CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
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 double ()
 
 operator float ()
 
 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 45 of file JamesRandom.h.

Constructor & Destructor Documentation

◆ HepJamesRandom() [1/4]

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

Definition at line 110 of file JamesRandom.cc.

112{
113 is >> *this;
114}

◆ HepJamesRandom() [2/4]

CLHEP::HepJamesRandom::HepJamesRandom ( )

Definition at line 77 of file JamesRandom.cc.

79{
80 long seeds[2];
81 long seed;
82
83 int numEngines = numberOfEngines++;
84 int cycle = std::abs(int(numEngines/maxIndex));
85 int curIndex = std::abs(int(numEngines%maxIndex));
86
87 long mask = ((cycle & 0x007fffff) << 8);
88 HepRandom::getTheTableSeeds( seeds, curIndex );
89 seed = seeds[0]^mask;
90 setSeed(seed,0);
91 setSeeds(&theSeed,0);
92}
void setSeeds(const long *seeds, int dum=0)
Definition: JamesRandom.cc:262
void setSeed(long seed, int dum=0)
Definition: JamesRandom.cc:210
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:256

◆ HepJamesRandom() [3/4]

CLHEP::HepJamesRandom::HepJamesRandom ( long  seed)

Definition at line 70 of file JamesRandom.cc.

72{
73 setSeed(seed,0);
74 setSeeds(&theSeed,0);
75}

◆ HepJamesRandom() [4/4]

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

Definition at line 94 of file JamesRandom.cc.

96{
97 long seed;
98 long seeds[2];
99
100 int cycle = std::abs(int(rowIndex/maxIndex));
101 int row = std::abs(int(rowIndex%maxIndex));
102 int col = std::abs(int(colIndex%2));
103 long mask = ((cycle & 0x000007ff) << 20);
104 HepRandom::getTheTableSeeds( seeds, row );
105 seed = (seeds[col])^mask;
106 setSeed(seed,0);
107 setSeeds(&theSeed,0);
108}

◆ ~HepJamesRandom()

CLHEP::HepJamesRandom::~HepJamesRandom ( )
virtual

Definition at line 116 of file JamesRandom.cc.

116{}

Member Function Documentation

◆ beginTag()

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

Definition at line 379 of file JamesRandom.cc.

379 {
380 return "JamesRandom-begin";
381}

◆ engineName()

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

Definition at line 93 of file JamesRandom.h.

93{return "HepJamesRandom";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::HepJamesRandom::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 268 of file JamesRandom.cc.

269{
270 double uni;
271
272 do {
273 uni = u[i97] - u[j97];
274 if ( uni < 0.0 ) uni++;
275 u[i97] = uni;
276
277 if (i97 == 0) i97 = 96;
278 else i97--;
279
280 if (j97 == 0) j97 = 96;
281 else j97--;
282
283 c -= cd;
284 if (c < 0.0) c += cm;
285
286 uni -= c;
287 if (uni < 0.0) uni += 1.0;
288 } while ( uni <= 0.0 || uni >= 1.0 );
289
290 return uni;
291}

Referenced by flatArray(), and main().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 293 of file JamesRandom.cc.

294{
295// double uni;
296 int i;
297
298 for (i=0; i<size; ++i) {
299 vect[i] = flat();
300 }
301}

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 427 of file JamesRandom.cc.

427 {
428 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
429 std::cerr <<
430 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
431 return false;
432 }
433 return getState(v);
434}
virtual std::istream & getState(std::istream &is)
Definition: JamesRandom.cc:383

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 362 of file JamesRandom.cc.

362 {
363 char beginMarker [MarkerLen];
364 is >> std::ws;
365 is.width(MarkerLen); // causes the next read to the char* to be <=
366 // that many bytes, INCLUDING A TERMINATION \0
367 // (Stroustrup, section 21.3.2)
368 is >> beginMarker;
369 if (strcmp(beginMarker,"JamesRandom-begin")) {
370 is.clear(std::ios::badbit | is.rdstate());
371 std::cerr << "\nInput stream mispositioned or"
372 << "\nJamesRandom state description missing or"
373 << "\nwrong engine type found." << std::endl;
374 return is;
375 }
376 return getState(is);
377}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 436 of file JamesRandom.cc.

436 {
437 if (v.size() != VECTOR_STATE_SIZE ) {
438 std::cerr <<
439 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
440 return false;
441 }
442 std::vector<unsigned long> t(2);
443 for (int i=0; i<97; ++i) {
444 t[0] = v[2*i+1]; t[1] = v[2*i+2];
445 u[i] = DoubConv::longs2double(t);
446 }
447 t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
448 t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
449 t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
450 j97 = (int)v[201];
451 i97 = (64+j97)%97;
452 return true;
453}
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static const unsigned int VECTOR_STATE_SIZE
Definition: JamesRandom.h:99

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 383 of file JamesRandom.cc.

383 {
384 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
385 std::vector<unsigned long> v;
386 unsigned long uu;
387 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
388 is >> uu;
389 if (!is) {
390 is.clear(std::ios::badbit | is.rdstate());
391 std::cerr << "\nJamesRandom state (vector) description improper."
392 << "\ngetState() has failed."
393 << "\nInput stream is probably mispositioned now." << std::endl;
394 return is;
395 }
396 v.push_back(uu);
397 }
398 getState(v);
399 return (is);
400 }
401
402// is >> theSeed; Removed, encompassed by possibleKeywordInput()
403
404 int ipos, jpos;
405 char endMarker [MarkerLen];
406 for (int i=0; i<97; ++i) {
407 is >> u[i];
408 }
409 is >> c; is >> cd; is >> cm;
410 is >> jpos;
411 is >> std::ws;
412 is.width(MarkerLen);
413 is >> endMarker;
414 if(strcmp(endMarker,"JamesRandom-end")) {
415 is.clear(std::ios::badbit | is.rdstate());
416 std::cerr << "\nJamesRandom state description incomplete."
417 << "\nInput stream is probably mispositioned now." << std::endl;
418 return is;
419 }
420
421 ipos = (64+jpos)%97;
422 i97 = ipos;
423 j97 = jpos;
424 return is;
425}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168

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

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 68 of file JamesRandom.cc.

68{return "HepJamesRandom";}

◆ operator double()

CLHEP::HepJamesRandom::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 303 of file JamesRandom.cc.

303 {
304 return flat();
305}

◆ operator float()

CLHEP::HepJamesRandom::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 307 of file JamesRandom.cc.

307 {
308 return float( flat() );
309}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 311 of file JamesRandom.cc.

311 {
312 return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
313 (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
314}
static double exponent_bit_32()

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 343 of file JamesRandom.cc.

343 {
344 std::vector<unsigned long> v;
345 v.push_back (engineIDulong<HepJamesRandom>());
346 std::vector<unsigned long> t;
347 for (int i=0; i<97; ++i) {
348 t = DoubConv::dto2longs(u[i]);
349 v.push_back(t[0]); v.push_back(t[1]);
350 }
351 t = DoubConv::dto2longs(c);
352 v.push_back(t[0]); v.push_back(t[1]);
353 t = DoubConv::dto2longs(cd);
354 v.push_back(t[0]); v.push_back(t[1]);
355 t = DoubConv::dto2longs(cm);
356 v.push_back(t[0]); v.push_back(t[1]);
357 v.push_back(static_cast<unsigned long>(j97));
358 return v;
359}
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

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 316 of file JamesRandom.cc.

316 {
317 char beginMarker[] = "JamesRandom-begin";
318 os << beginMarker << "\nUvec\n";
319 std::vector<unsigned long> v = put();
320 for (unsigned int i=0; i<v.size(); ++i) {
321 os << v[i] << "\n";
322 }
323 return os;
324#ifdef REMOVED
325 char endMarker[] = "JamesRandom-end";
326 int pos = j97;
327 long pr = os.precision(20);
328 os << " " << beginMarker << " ";
329 os << theSeed << " ";
330 for (int i=0; i<97; ++i) {
331 os << std::setprecision(20) << u[i] << "\n";
332 }
333 os << std::setprecision(20) << c << " ";
334 os << std::setprecision(20) << cd << " ";
335 os << std::setprecision(20) << cm << " ";
336 os << pos << "\n";
337 os << endMarker << "\n";
338 os.precision(pr);
339 return os;
340#endif
341}
std::vector< unsigned long > put() const
Definition: JamesRandom.cc:343

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 152 of file JamesRandom.cc.

153{
154 int ipos, jpos;
155 std::ifstream inFile( filename, std::ios::in);
156 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
157 std::cerr << " -- Engine state remains unchanged\n";
158 return;
159 }
160 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
161 std::vector<unsigned long> v;
162 unsigned long xin;
163 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
164 inFile >> xin;
165 #ifdef TRACE_IO
166 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
167 if (ivec%3 == 0) std::cout << "\n";
168 #endif
169 if (!inFile) {
170 inFile.clear(std::ios::badbit | inFile.rdstate());
171 std::cerr << "\nJamesRandom state (vector) description improper."
172 << "\nrestoreStatus has failed."
173 << "\nInput stream is probably mispositioned now." << std::endl;
174 return;
175 }
176 v.push_back(xin);
177 }
178 getState(v);
179 return;
180 }
181
182 if (!inFile.bad() && !inFile.eof()) {
183// inFile >> theSeed; removed -- encompased by possibleKeywordInput
184 for (int i=0; i<97; ++i)
185 inFile >> u[i];
186 inFile >> c; inFile >> cd; inFile >> cm;
187 inFile >> jpos;
188 ipos = (64+jpos)%97;
189 i97 = ipos;
190 j97 = jpos;
191 }
192}
static std::string engineName()
Definition: JamesRandom.h:93
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 118 of file JamesRandom.cc.

119{
120 std::ofstream outFile( filename, std::ios::out ) ;
121
122 if (!outFile.bad()) {
123 outFile << "Uvec\n";
124 std::vector<unsigned long> v = put();
125 #ifdef TRACE_IO
126 std::cout << "Result of v = put() is:\n";
127 #endif
128 for (unsigned int i=0; i<v.size(); ++i) {
129 outFile << v[i] << "\n";
130 #ifdef TRACE_IO
131 std::cout << v[i] << " ";
132 if (i%6==0) std::cout << "\n";
133 #endif
134 }
135 #ifdef TRACE_IO
136 std::cout << "\n";
137 #endif
138 }
139#ifdef REMOVED
140 int pos = j97;
141 outFile << theSeed << std::endl;
142 for (int i=0; i<97; ++i)
143 outFile << std::setprecision(20) << u[i] << " ";
144 outFile << std::endl;
145 outFile << std::setprecision(20) << c << " ";
146 outFile << std::setprecision(20) << cd << " ";
147 outFile << std::setprecision(20) << cm << std::endl;
148 outFile << pos << std::endl;
149#endif
150}

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 210 of file JamesRandom.cc.

211{
212 // The input value for "seed" should be within the range [0,900000000]
213 //
214 // Negative seeds result in serious flaws in the randomness;
215 // seeds above 900000000 are OK because of the %177 in the expression for i,
216 // but may have the same effect as other seeds below 900000000.
217
218 int m, n;
219 float s, t;
220 long mm;
221
222 if (seed < 0) {
223 std::cout << "Seed for HepJamesRandom must be non-negative\n"
224 << "Seed value supplied was " << seed
225 << "\nUsing its absolute value instead\n";
226 seed = -seed;
227 }
228
229 long ij = seed/30082;
230 long kl = seed - 30082*ij;
231 long i = (ij/177) % 177 + 2;
232 long j = ij % 177 + 2;
233 long k = (kl/169) % 178 + 1;
234 long l = kl % 169;
235
236 theSeed = seed;
237
238 for ( n = 1 ; n < 98 ; n++ ) {
239 s = 0.0;
240 t = 0.5;
241 for ( m = 1 ; m < 25 ; m++) {
242 mm = ( ( (i*j) % 179 ) * k ) % 179;
243 i = j;
244 j = k;
245 k = mm;
246 l = ( 53 * l + 1 ) % 169;
247 if ( (l*mm % 64 ) >= 32 )
248 s += t;
249 t *= 0.5;
250 }
251 u[n-1] = s;
252 }
253 c = 362436.0 / 16777216.0;
254 cd = 7654321.0 / 16777216.0;
255 cm = 16777213.0 / 16777216.0;
256
257 i97 = 96;
258 j97 = 32;
259
260}

Referenced by HepJamesRandom(), and setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 262 of file JamesRandom.cc.

263{
264 setSeed(seeds ? *seeds : 19780503L, 0);
265 theSeeds = seeds;
266}

Referenced by HepJamesRandom().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 194 of file JamesRandom.cc.

195{
196 std::cout << std::endl;
197 std::cout << "----- HepJamesRandom engine status -----" << std::endl;
198 std::cout << " Initial seed = " << theSeed << std::endl;
199 std::cout << " u[] = ";
200 for (int i=0; i<97; ++i)
201 std::cout << u[i] << " ";
202 std::cout << std::endl;
203 std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
204 << std::endl;
205 std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
206 std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
207 std::cout << "----------------------------------------" << std::endl;
208}

Member Data Documentation

◆ VECTOR_STATE_SIZE

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

Definition at line 99 of file JamesRandom.h.

Referenced by getState(), and restoreStatus().


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