CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
JamesRandom.cc
Go to the documentation of this file.
1//
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- HepJamesRandom ---
7// class implementation file
8// -----------------------------------------------------------------------
9// This file is part of Geant4 (simulation toolkit for HEP).
10//
11// This algorithm implements the original universal random number generator
12// as proposed by Marsaglia & Zaman in report FSU-SCRI-87-50 and coded
13// in FORTRAN77 by Fred James as the RANMAR generator, part of the MATHLIB
14// HEP library.
15
16// =======================================================================
17// Gabriele Cosmo - Created: 5th September 1995
18// - Fixed a bug in setSeed(): 26th February 1996
19// - Minor corrections: 31st October 1996
20// - Added methods for engine status: 19th November 1996
21// - Fixed bug in setSeeds(): 15th September 1997
22// J.Marraffino - Added stream operators and related constructor.
23// Added automatic seed selection from seed table and
24// engine counter: 16th Feb 1998
25// Ken Smith - Added conversion operators: 6th Aug 1998
26// J. Marraffino - Remove dependence on hepString class 13 May 1999
27// V. Innocente - changed pointers to indices 3 may 2000
28// M. Fischler - In restore, checkFile for file not found 03 Dec 2004
29// M. Fischler - Methods for distrib. instacne save/restore 12/8/04
30// M. Fischler - split get() into tag validation and
31// getState() for anonymous restores 12/27/04
32// M. Fischler - Enforcement that seeds be non-negative
33// (lest the sequence be non-random) 2/14/05
34// M. Fischler - put/get for vectors of ulongs 3/14/05
35// M. Fischler - State-saving using only ints, for portability 4/12/05
36//
37// =======================================================================
38
39#include "CLHEP/Random/defs.h"
40#include "CLHEP/Random/Random.h"
41#include "CLHEP/Random/JamesRandom.h"
42#include "CLHEP/Random/engineIDulong.h"
43#include "CLHEP/Random/DoubConv.h"
44#include "CLHEP/Utility/atomic_int.h"
45
46#include <atomic>
47#include <cmath>
48#include <cstdlib>
49#include <iostream>
50#include <string.h> // for strcmp
51#include <string>
52#include <vector>
53
54//#define TRACE_IO
55
56namespace CLHEP {
57
58namespace {
59 // Number of instances with automatic seed selection
60 CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
61
62 // Maximum index into the seed table
63 const int maxIndex = 215;
64}
65
66static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
67
68std::string HepJamesRandom::name() const {return "HepJamesRandom";}
69
72{
73 setSeed(seed,0);
74 setSeeds(&theSeed,0);
75}
76
77HepJamesRandom::HepJamesRandom() // 15 Feb. 1998 JMM
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}
93
94HepJamesRandom::HepJamesRandom(int rowIndex, int colIndex) // 15 Feb. 1998 JMM
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}
109
112{
113 is >> *this;
114}
115
117
118void HepJamesRandom::saveStatus( const char filename[] ) const
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}
151
152void HepJamesRandom::restoreStatus( const char filename[] )
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}
193
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}
209
210void HepJamesRandom::setSeed(long seed, int)
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}
261
262void HepJamesRandom::setSeeds(const long* seeds, int)
263{
264 setSeed(seeds ? *seeds : 19780503L, 0);
265 theSeeds = seeds;
266}
267
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}
292
293void HepJamesRandom::flatArray(const int size, double* vect)
294{
295// double uni;
296 int i;
297
298 for (i=0; i<size; ++i) {
299 vect[i] = flat();
300 }
301}
302
303HepJamesRandom::operator double() {
304 return flat();
305}
306
307HepJamesRandom::operator float() {
308 return float( flat() );
309}
310
311HepJamesRandom::operator unsigned int() {
312 return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
313 (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
314}
315
316std::ostream & HepJamesRandom::put ( std::ostream& os ) const {
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}
342
343std::vector<unsigned long> HepJamesRandom::put () const {
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}
360
361
362std::istream & HepJamesRandom::get ( std::istream& is) {
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}
378
379std::string HepJamesRandom::beginTag ( ) {
380 return "JamesRandom-begin";
381}
382
383std::istream & HepJamesRandom::getState ( std::istream& is) {
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}
426
427bool HepJamesRandom::get (const std::vector<unsigned long> & v) {
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}
435
436bool HepJamesRandom::getState (const std::vector<unsigned long> & v) {
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}
454
455} // namespace CLHEP
#define CLHEP_ATOMIC_INT_TYPE
Definition: atomic_int.h:25
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
void flatArray(const int size, double *vect)
Definition: JamesRandom.cc:293
static std::string beginTag()
Definition: JamesRandom.cc:379
static std::string engineName()
Definition: JamesRandom.h:93
void setSeeds(const long *seeds, int dum=0)
Definition: JamesRandom.cc:262
virtual std::istream & getState(std::istream &is)
Definition: JamesRandom.cc:383
std::string name() const
Definition: JamesRandom.cc:68
std::vector< unsigned long > put() const
Definition: JamesRandom.cc:343
void setSeed(long seed, int dum=0)
Definition: JamesRandom.cc:210
virtual std::istream & get(std::istream &is)
Definition: JamesRandom.cc:362
void saveStatus(const char filename[]="JamesRand.conf") const
Definition: JamesRandom.cc:118
void restoreStatus(const char filename[]="JamesRand.conf")
Definition: JamesRandom.cc:152
static const unsigned int VECTOR_STATE_SIZE
Definition: JamesRandom.h:99
void showStatus() const
Definition: JamesRandom.cc:194
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:256
#define double(obj)
Definition: excDblThrow.cc:32
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168