Geant4  10.02.p02
JamesRandom.cc
Go to the documentation of this file.
1 // $Id:$
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/Random.h"
40 #include "CLHEP/Random/JamesRandom.h"
41 #include "CLHEP/Random/engineIDulong.h"
42 #include "CLHEP/Random/DoubConv.h"
43 #include "CLHEP/Utility/atomic_int.h"
44 
45 #include <string.h> // for strcmp
46 #include <cmath>
47 #include <cstdlib>
48 
49 namespace CLHEP {
50 
51 namespace {
52  // Number of instances with automatic seed selection
53  CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
54 
55  // Maximum index into the seed table
56  const int maxIndex = 215;
57 }
58 
59 static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
60 
61 std::string HepJamesRandom::name() const {return "HepJamesRandom";}
62 
63 HepJamesRandom::HepJamesRandom(long seed)
64 : HepRandomEngine()
65 {
66  setSeed(seed,0);
67  setSeeds(&theSeed,0);
68 }
69 
70 HepJamesRandom::HepJamesRandom() // 15 Feb. 1998 JMM
71 : HepRandomEngine()
72 {
73  long seeds[2];
74  long seed;
75 
76  int numEngines = numberOfEngines++;
77  int cycle = std::abs(int(numEngines/maxIndex));
78  int curIndex = std::abs(int(numEngines%maxIndex));
79 
80  long mask = ((cycle & 0x007fffff) << 8);
81  HepRandom::getTheTableSeeds( seeds, curIndex );
82  seed = seeds[0]^mask;
83  setSeed(seed,0);
84  setSeeds(&theSeed,0);
85 }
86 
87 HepJamesRandom::HepJamesRandom(int rowIndex, int colIndex) // 15 Feb. 1998 JMM
88 : HepRandomEngine()
89 {
90  long seed;
91  long seeds[2];
92 
93  int cycle = std::abs(int(rowIndex/maxIndex));
94  int row = std::abs(int(rowIndex%maxIndex));
95  int col = std::abs(int(colIndex%2));
96  long mask = ((cycle & 0x000007ff) << 20);
97  HepRandom::getTheTableSeeds( seeds, row );
98  seed = (seeds[col])^mask;
99  setSeed(seed,0);
100  setSeeds(&theSeed,0);
101 }
102 
103 HepJamesRandom::HepJamesRandom(std::istream& is)
104 : HepRandomEngine()
105 {
106  is >> *this;
107 }
108 
109 HepJamesRandom::~HepJamesRandom() {}
110 
111 void HepJamesRandom::saveStatus( const char filename[] ) const
112 {
113  std::ofstream outFile( filename, std::ios::out ) ;
114 
115  if (!outFile.bad()) {
116  outFile << "Uvec\n";
117  std::vector<unsigned long> v = put();
118  for (unsigned int i=0; i<v.size(); ++i) {
119  outFile << v[i] << "\n";
120  }
121  }
122 }
123 
124 void HepJamesRandom::restoreStatus( const char filename[] )
125 {
126  int ipos, jpos;
127  std::ifstream inFile( filename, std::ios::in);
128  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
129  std::cerr << " -- Engine state remains unchanged\n";
130  return;
131  }
132  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
133  std::vector<unsigned long> v;
134  unsigned long xin;
135  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
136  inFile >> xin;
137  if (!inFile) {
138  inFile.clear(std::ios::badbit | inFile.rdstate());
139  std::cerr << "\nJamesRandom state (vector) description improper."
140  << "\nrestoreStatus has failed."
141  << "\nInput stream is probably mispositioned now." << std::endl;
142  return;
143  }
144  v.push_back(xin);
145  }
146  getState(v);
147  return;
148  }
149 
150  if (!inFile.bad() && !inFile.eof()) {
151 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
152  for (int i=0; i<97; ++i)
153  inFile >> u[i];
154  inFile >> c; inFile >> cd; inFile >> cm;
155  inFile >> jpos;
156  ipos = (64+jpos)%97;
157  i97 = ipos;
158  j97 = jpos;
159  }
160 }
161 
162 void HepJamesRandom::showStatus() const
163 {
164  std::cout << std::endl;
165  std::cout << "----- HepJamesRandom engine status -----" << std::endl;
166  std::cout << " Initial seed = " << theSeed << std::endl;
167  std::cout << " u[] = ";
168  for (int i=0; i<97; ++i)
169  std::cout << u[i] << " ";
170  std::cout << std::endl;
171  std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
172  << std::endl;
173  std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
174  std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
175  std::cout << "----------------------------------------" << std::endl;
176 }
177 
178 void HepJamesRandom::setSeed(long seed, int)
179 {
180  // The input value for "seed" should be within the range [0,900000000]
181  //
182  // Negative seeds result in serious flaws in the randomness;
183  // seeds above 900000000 are OK because of the %177 in the expression for i,
184  // but may have the same effect as other seeds below 900000000.
185 
186  int m, n;
187  float s, t;
188  long mm;
189 
190  if (seed < 0) {
191  std::cout << "Seed for HepJamesRandom must be non-negative\n"
192  << "Seed value supplied was " << seed
193  << "\nUsing its absolute value instead\n";
194  seed = -seed;
195  }
196 
197  long ij = seed/30082;
198  long kl = seed - 30082*ij;
199  long i = (ij/177) % 177 + 2;
200  long j = ij % 177 + 2;
201  long k = (kl/169) % 178 + 1;
202  long l = kl % 169;
203 
204  theSeed = seed;
205 
206  for ( n = 1 ; n < 98 ; n++ ) {
207  s = 0.0;
208  t = 0.5;
209  for ( m = 1 ; m < 25 ; m++) {
210  mm = ( ( (i*j) % 179 ) * k ) % 179;
211  i = j;
212  j = k;
213  k = mm;
214  l = ( 53 * l + 1 ) % 169;
215  if ( (l*mm % 64 ) >= 32 )
216  s += t;
217  t *= 0.5;
218  }
219  u[n-1] = s;
220  }
221  c = 362436.0 / 16777216.0;
222  cd = 7654321.0 / 16777216.0;
223  cm = 16777213.0 / 16777216.0;
224 
225  i97 = 96;
226  j97 = 32;
227 
228 }
229 
230 void HepJamesRandom::setSeeds(const long* seeds, int)
231 {
232  setSeed(seeds ? *seeds : 19780503L, 0);
233  theSeeds = seeds;
234 }
235 
236 double HepJamesRandom::flat()
237 {
238  double uni;
239 
240  do {
241  uni = u[i97] - u[j97];
242  if ( uni < 0.0 ) uni++;
243  u[i97] = uni;
244 
245  if (i97 == 0) i97 = 96;
246  else i97--;
247 
248  if (j97 == 0) j97 = 96;
249  else j97--;
250 
251  c -= cd;
252  if (c < 0.0) c += cm;
253 
254  uni -= c;
255  if (uni < 0.0) uni += 1.0;
256  } while ( uni <= 0.0 || uni >= 1.0 );
257 
258  return uni;
259 }
260 
261 void HepJamesRandom::flatArray(const int size, double* vect)
262 {
263 // double uni;
264  int i;
265 
266  for (i=0; i<size; ++i) {
267  vect[i] = flat();
268  }
269 }
270 
271 HepJamesRandom::operator unsigned int() {
272  return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
273  (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
274 }
275 
276 std::ostream & HepJamesRandom::put ( std::ostream& os ) const {
277  char beginMarker[] = "JamesRandom-begin";
278  os << beginMarker << "\nUvec\n";
279  std::vector<unsigned long> v = put();
280  for (unsigned int i=0; i<v.size(); ++i) {
281  os << v[i] << "\n";
282  }
283  return os;
284 }
285 
286 std::vector<unsigned long> HepJamesRandom::put () const {
287  std::vector<unsigned long> v;
288  v.push_back (engineIDulong<HepJamesRandom>());
289  std::vector<unsigned long> t;
290  for (int i=0; i<97; ++i) {
291  t = DoubConv::dto2longs(u[i]);
292  v.push_back(t[0]); v.push_back(t[1]);
293  }
294  t = DoubConv::dto2longs(c);
295  v.push_back(t[0]); v.push_back(t[1]);
296  t = DoubConv::dto2longs(cd);
297  v.push_back(t[0]); v.push_back(t[1]);
298  t = DoubConv::dto2longs(cm);
299  v.push_back(t[0]); v.push_back(t[1]);
300  v.push_back(static_cast<unsigned long>(j97));
301  return v;
302 }
303 
304 
305 std::istream & HepJamesRandom::get ( std::istream& is) {
306  char beginMarker [MarkerLen];
307  is >> std::ws;
308  is.width(MarkerLen); // causes the next read to the char* to be <=
309  // that many bytes, INCLUDING A TERMINATION \0
310  // (Stroustrup, section 21.3.2)
311  is >> beginMarker;
312  if (strcmp(beginMarker,"JamesRandom-begin")) {
313  is.clear(std::ios::badbit | is.rdstate());
314  std::cerr << "\nInput stream mispositioned or"
315  << "\nJamesRandom state description missing or"
316  << "\nwrong engine type found." << std::endl;
317  return is;
318  }
319  return getState(is);
320 }
321 
322 std::string HepJamesRandom::beginTag ( ) {
323  return "JamesRandom-begin";
324 }
325 
326 std::istream & HepJamesRandom::getState ( std::istream& is) {
327  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
328  std::vector<unsigned long> v;
329  unsigned long uu;
330  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
331  is >> uu;
332  if (!is) {
333  is.clear(std::ios::badbit | is.rdstate());
334  std::cerr << "\nJamesRandom state (vector) description improper."
335  << "\ngetState() has failed."
336  << "\nInput stream is probably mispositioned now." << std::endl;
337  return is;
338  }
339  v.push_back(uu);
340  }
341  getState(v);
342  return (is);
343  }
344 
345 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
346 
347  int ipos, jpos;
348  char endMarker [MarkerLen];
349  for (int i=0; i<97; ++i) {
350  is >> u[i];
351  }
352  is >> c; is >> cd; is >> cm;
353  is >> jpos;
354  is >> std::ws;
355  is.width(MarkerLen);
356  is >> endMarker;
357  if(strcmp(endMarker,"JamesRandom-end")) {
358  is.clear(std::ios::badbit | is.rdstate());
359  std::cerr << "\nJamesRandom state description incomplete."
360  << "\nInput stream is probably mispositioned now." << std::endl;
361  return is;
362  }
363 
364  ipos = (64+jpos)%97;
365  i97 = ipos;
366  j97 = jpos;
367  return is;
368 }
369 
370 bool HepJamesRandom::get (const std::vector<unsigned long> & v) {
371  if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
372  std::cerr <<
373  "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
374  return false;
375  }
376  return getState(v);
377 }
378 
379 bool HepJamesRandom::getState (const std::vector<unsigned long> & v) {
380  if (v.size() != VECTOR_STATE_SIZE ) {
381  std::cerr <<
382  "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
383  return false;
384  }
385  std::vector<unsigned long> t(2);
386  for (int i=0; i<97; ++i) {
387  t[0] = v[2*i+1]; t[1] = v[2*i+2];
388  u[i] = DoubConv::longs2double(t);
389  }
390  t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
391  t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
392  t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
393  j97 = v[201];
394  i97 = (64+j97)%97;
395  return true;
396 }
397 
398 } // namespace CLHEP
static const double cm
Definition: G4SIunits.hh:118
static const int MarkerLen
Definition: DualRand.cc:67
static ush mask[]
Definition: csz_inflate.cc:317
G4String name
Definition: TRTMaterials.hh:40
static const G4double cd
static const double L
Definition: G4SIunits.hh:123
static const double s
Definition: G4SIunits.hh:168
const G4int n
double flat()
Definition: G4AblaRandom.cc:47
static const double m
Definition: G4SIunits.hh:128
static const double mm
Definition: G4SIunits.hh:114
void setSeeds(const SeedVector &sv)
Set the seeds of the current generator.
Definition: G4INCLRandom.cc:85