Geant4 11.2.2
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 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)
 
long getSeed () const
 
const long * getSeeds () const
 

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 43 of file JamesRandom.h.

Constructor & Destructor Documentation

◆ HepJamesRandom() [1/4]

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

Definition at line 106 of file JamesRandom.cc.

108{
109 is >> *this;
110}

◆ HepJamesRandom() [2/4]

CLHEP::HepJamesRandom::HepJamesRandom ( )

Definition at line 73 of file JamesRandom.cc.

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

◆ HepJamesRandom() [3/4]

CLHEP::HepJamesRandom::HepJamesRandom ( long seed)

Definition at line 66 of file JamesRandom.cc.

68{
69 setSeed(seed,0);
70 setSeeds(&theSeed,0);
71}

◆ HepJamesRandom() [4/4]

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

Definition at line 90 of file JamesRandom.cc.

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

◆ ~HepJamesRandom()

CLHEP::HepJamesRandom::~HepJamesRandom ( )
virtual

Definition at line 112 of file JamesRandom.cc.

112{}

Member Function Documentation

◆ beginTag()

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

Definition at line 333 of file JamesRandom.cc.

333 {
334 return "JamesRandom-begin";
335}

◆ engineName()

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

Definition at line 91 of file JamesRandom.h.

91{return "HepJamesRandom";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::HepJamesRandom::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 239 of file JamesRandom.cc.

240{
241 double uni;
242
243 do {
244 uni = u[i97] - u[j97];
245 if ( uni < 0.0 ) uni++;
246 u[i97] = uni;
247
248 if (i97 == 0) i97 = 96;
249 else i97--;
250
251 if (j97 == 0) j97 = 96;
252 else j97--;
253
254 c -= cd;
255 if (c < 0.0) c += cm;
256
257 uni -= c;
258 if (uni < 0.0) uni += 1.0;
259 } while ( uni <= 0.0 || uni >= 1.0 );
260
261 return uni;
262}

Referenced by flatArray().

◆ flatArray()

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

Implements CLHEP::HepRandomEngine.

Definition at line 264 of file JamesRandom.cc.

265{
266// double uni;
267 int i;
268
269 for (i=0; i<size; ++i) {
270 vect[i] = flat();
271 }
272}

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 381 of file JamesRandom.cc.

381 {
382 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
383 std::cerr <<
384 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
385 return false;
386 }
387 return getState(v);
388}
virtual std::istream & getState(std::istream &is)
unsigned long engineIDulong()

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 316 of file JamesRandom.cc.

316 {
317 char beginMarker [MarkerLen];
318 is >> std::ws;
319 is.width(MarkerLen); // causes the next read to the char* to be <=
320 // that many bytes, INCLUDING A TERMINATION \0
321 // (Stroustrup, section 21.3.2)
322 is >> beginMarker;
323 if (strcmp(beginMarker,"JamesRandom-begin")) {
324 is.clear(std::ios::badbit | is.rdstate());
325 std::cerr << "\nInput stream mispositioned or"
326 << "\nJamesRandom state description missing or"
327 << "\nwrong engine type found." << std::endl;
328 return is;
329 }
330 return getState(is);
331}

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 390 of file JamesRandom.cc.

390 {
391 if (v.size() != VECTOR_STATE_SIZE ) {
392 std::cerr <<
393 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
394 return false;
395 }
396 std::vector<unsigned long> t(2);
397 for (int i=0; i<97; ++i) {
398 t[0] = v[2*i+1]; t[1] = v[2*i+2];
399 u[i] = DoubConv::longs2double(t);
400 }
401 t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
402 t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
403 t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
404 j97 = (int)v[201];
405 i97 = (64+j97)%97;
406 return true;
407}
static double longs2double(const std::vector< unsigned long > &v)
Definition DoubConv.cc:110
static const unsigned int VECTOR_STATE_SIZE
Definition JamesRandom.h:97

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 337 of file JamesRandom.cc.

337 {
338 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
339 std::vector<unsigned long> v;
340 unsigned long uu;
341 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
342 is >> uu;
343 if (!is) {
344 is.clear(std::ios::badbit | is.rdstate());
345 std::cerr << "\nJamesRandom state (vector) description improper."
346 << "\ngetState() has failed."
347 << "\nInput stream is probably mispositioned now." << std::endl;
348 return is;
349 }
350 v.push_back(uu);
351 }
352 getState(v);
353 return (is);
354 }
355
356// is >> theSeed; Removed, encompassed by possibleKeywordInput()
357
358 int ipos, jpos;
359 char endMarker [MarkerLen];
360 for (int i=0; i<97; ++i) {
361 is >> u[i];
362 }
363 is >> c; is >> cd; is >> cm;
364 is >> jpos;
365 is >> std::ws;
366 is.width(MarkerLen);
367 is >> endMarker;
368 if(strcmp(endMarker,"JamesRandom-end")) {
369 is.clear(std::ios::badbit | is.rdstate());
370 std::cerr << "\nJamesRandom state description incomplete."
371 << "\nInput stream is probably mispositioned now." << std::endl;
372 return is;
373 }
374
375 ipos = (64+jpos)%97;
376 i97 = ipos;
377 j97 = jpos;
378 return is;
379}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)

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

◆ name()

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

Implements CLHEP::HepRandomEngine.

Definition at line 64 of file JamesRandom.cc.

64{return "HepJamesRandom";}

◆ operator double()

CLHEP::HepJamesRandom::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 274 of file JamesRandom.cc.

274 {
275 return flat();
276}

◆ operator float()

CLHEP::HepJamesRandom::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 278 of file JamesRandom.cc.

278 {
279 return float( flat() );
280}

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 282 of file JamesRandom.cc.

282 {
283 return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
284 (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
285}
static double exponent_bit_32()

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 297 of file JamesRandom.cc.

297 {
298 std::vector<unsigned long> v;
299 v.push_back (engineIDulong<HepJamesRandom>());
300 std::vector<unsigned long> t;
301 for (int i=0; i<97; ++i) {
302 t = DoubConv::dto2longs(u[i]);
303 v.push_back(t[0]); v.push_back(t[1]);
304 }
305 t = DoubConv::dto2longs(c);
306 v.push_back(t[0]); v.push_back(t[1]);
307 t = DoubConv::dto2longs(cd);
308 v.push_back(t[0]); v.push_back(t[1]);
309 t = DoubConv::dto2longs(cm);
310 v.push_back(t[0]); v.push_back(t[1]);
311 v.push_back(static_cast<unsigned long>(j97));
312 return v;
313}
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 287 of file JamesRandom.cc.

287 {
288 char beginMarker[] = "JamesRandom-begin";
289 os << beginMarker << "\nUvec\n";
290 std::vector<unsigned long> v = put();
291 for (unsigned int i=0; i<v.size(); ++i) {
292 os << v[i] << "\n";
293 }
294 return os;
295}
std::vector< unsigned long > put() const

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 127 of file JamesRandom.cc.

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

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 114 of file JamesRandom.cc.

115{
116 std::ofstream outFile( filename, std::ios::out ) ;
117
118 if (!outFile.bad()) {
119 outFile << "Uvec\n";
120 std::vector<unsigned long> v = put();
121 for (unsigned int i=0; i<v.size(); ++i) {
122 outFile << v[i] << "\n";
123 }
124 }
125}

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 181 of file JamesRandom.cc.

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

Referenced by HepJamesRandom(), HepJamesRandom(), HepJamesRandom(), and setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 233 of file JamesRandom.cc.

234{
235 setSeed(seeds ? *seeds : 19780503L, 0);
236 theSeeds = seeds;
237}

Referenced by HepJamesRandom(), HepJamesRandom(), and HepJamesRandom().

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 165 of file JamesRandom.cc.

166{
167 std::cout << std::endl;
168 std::cout << "----- HepJamesRandom engine status -----" << std::endl;
169 std::cout << " Initial seed = " << theSeed << std::endl;
170 std::cout << " u[] = ";
171 for (int i=0; i<97; ++i)
172 std::cout << u[i] << " ";
173 std::cout << std::endl;
174 std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
175 << std::endl;
176 std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
177 std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
178 std::cout << "----------------------------------------" << std::endl;
179}

Member Data Documentation

◆ VECTOR_STATE_SIZE

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

Definition at line 97 of file JamesRandom.h.

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


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