Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CLHEP::MTwistEngine Class Reference

#include <MTwistEngine.h>

Inheritance diagram for CLHEP::MTwistEngine:
Collaboration diagram for CLHEP::MTwistEngine:

Public Member Functions

 MTwistEngine ()
 
 MTwistEngine (long seed)
 
 MTwistEngine (int rowIndex, int colIndex)
 
 MTwistEngine (std::istream &is)
 
virtual ~MTwistEngine ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int)
 
void setSeeds (const long *seeds, int)
 
void saveStatus (const char filename[]="MTwist.conf") const
 
void restoreStatus (const char filename[]="MTwist.conf")
 
void showStatus () const
 
 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
 
virtual operator double ()
 

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 = 626
 

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 35 of file MTwistEngine.h.

Constructor & Destructor Documentation

CLHEP::MTwistEngine::MTwistEngine ( )

Definition at line 64 of file MTwistEngine.cc.

66 {
67  int numEngines = numberOfEngines++;
68  int cycle = std::abs(int(numEngines/maxIndex));
69  int curIndex = std::abs(int(numEngines%maxIndex));
70  long mask = ((cycle & 0x007fffff) << 8);
71  long seedlist[2];
72  HepRandom::getTheTableSeeds( seedlist, curIndex );
73  seedlist[0] = (seedlist[0])^mask;
74  seedlist[1] = 0;
75  setSeeds( seedlist, numEngines );
76  count624=0;
77 
78  for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
79 }
void setSeeds(const long *seeds, int)
static ush mask[]
Definition: csz_inflate.cc:317
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:251

Here is the call graph for this function:

CLHEP::MTwistEngine::MTwistEngine ( long  seed)

Definition at line 81 of file MTwistEngine.cc.

83 {
84  long seedlist[2]={seed,17587};
85  setSeeds( seedlist, 0 );
86  count624=0;
87  for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
88 }
void setSeeds(const long *seeds, int)

Here is the call graph for this function:

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

Definition at line 90 of file MTwistEngine.cc.

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  long seedlist[2];
98  HepRandom::getTheTableSeeds( seedlist, row );
99  seedlist[0] = (seedlist[col])^mask;
100  seedlist[1] = 690691;
101  setSeeds(seedlist, 4444772);
102  count624=0;
103  for( int i=0; i < 2000; ++i ) flat(); // Warm up just a bit
104 }
void setSeeds(const long *seeds, int)
static ush mask[]
Definition: csz_inflate.cc:317
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:251

Here is the call graph for this function:

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

Definition at line 106 of file MTwistEngine.cc.

107 : HepRandomEngine()
108 {
109  is >> *this;
110 }
CLHEP::MTwistEngine::~MTwistEngine ( )
virtual

Definition at line 112 of file MTwistEngine.cc.

112 {}

Member Function Documentation

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

Definition at line 340 of file MTwistEngine.cc.

340  {
341  return "MTwistEngine-begin";
342 }
static std::string CLHEP::MTwistEngine::engineName ( )
inlinestatic

Definition at line 77 of file MTwistEngine.h.

77 {return "MTwistEngine";}

Here is the caller graph for this function:

double CLHEP::MTwistEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 114 of file MTwistEngine.cc.

114  {
115  unsigned int y;
116 
117  if( count624 >= N ) {
118  int i;
119 
120  for( i=0; i < NminusM; ++i ) {
121  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
122  mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
123  }
124 
125  for( ; i < N-1 ; ++i ) {
126  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
127  mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
128  }
129 
130  y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
131  mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
132 
133  count624 = 0;
134  }
135 
136  y = mt[count624];
137  y ^= ( y >> 11);
138  y ^= ((y << 7 ) & 0x9d2c5680);
139  y ^= ((y << 15) & 0xefc60000);
140  y ^= ( y >> 18);
141 
142  return y * twoToMinus_32() + // Scale to range
143  (mt[count624++] >> 11) * twoToMinus_53() + // fill remaining bits
144  nearlyTwoToMinus_54(); // make sure non-zero
145 }
static double nearlyTwoToMinus_54()
const int N
Definition: mixmax.h:43
static double twoToMinus_32()
static double twoToMinus_53()

Here is the call graph for this function:

Here is the caller graph for this function:

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

Implements CLHEP::HepRandomEngine.

Definition at line 147 of file MTwistEngine.cc.

147  {
148  for( int i=0; i < size; ++i) vect[i] = flat();
149 }

Here is the call graph for this function:

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 322 of file MTwistEngine.cc.

323 {
324  char beginMarker [MarkerLen];
325  is >> std::ws;
326  is.width(MarkerLen); // causes the next read to the char* to be <=
327  // that many bytes, INCLUDING A TERMINATION \0
328  // (Stroustrup, section 21.3.2)
329  is >> beginMarker;
330  if (strcmp(beginMarker,"MTwistEngine-begin")) {
331  is.clear(std::ios::badbit | is.rdstate());
332  std::cerr << "\nInput stream mispositioned or"
333  << "\nMTwistEngine state description missing or"
334  << "\nwrong engine type found." << std::endl;
335  return is;
336  }
337  return getState(is);
338 }
static const int MarkerLen
Definition: DualRand.cc:67
virtual std::istream & getState(std::istream &is)

Here is the call graph for this function:

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 362 of file MTwistEngine.cc.

362  {
363  if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
364  std::cerr <<
365  "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
366  return false;
367  }
368  return getState(v);
369 }
virtual std::istream & getState(std::istream &is)

Here is the call graph for this function:

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 344 of file MTwistEngine.cc.

345 {
346  char endMarker [MarkerLen];
347  is >> theSeed;
348  for (int i=0; i<624; ++i) is >> mt[i];
349  is >> count624;
350  is >> std::ws;
351  is.width(MarkerLen);
352  is >> endMarker;
353  if (strcmp(endMarker,"MTwistEngine-end")) {
354  is.clear(std::ios::badbit | is.rdstate());
355  std::cerr << "\nMTwistEngine state description incomplete."
356  << "\nInput stream is probably mispositioned now." << std::endl;
357  return is;
358  }
359  return is;
360 }
static const int MarkerLen
Definition: DualRand.cc:67

Here is the caller graph for this function:

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 371 of file MTwistEngine.cc.

371  {
372  if (v.size() != VECTOR_STATE_SIZE ) {
373  std::cerr <<
374  "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
375  return false;
376  }
377  for (int i=0; i<624; ++i) {
378  mt[i]=v[i+1];
379  }
380  count624 = v[625];
381  return true;
382 }
static const unsigned int VECTOR_STATE_SIZE
Definition: MTwistEngine.h:83
std::string CLHEP::MTwistEngine::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 62 of file MTwistEngine.cc.

62 {return "MTwistEngine";}
CLHEP::MTwistEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 233 of file MTwistEngine.cc.

233  {
234  unsigned int y;
235 
236  if( count624 >= N ) {
237  int i;
238 
239  for( i=0; i < NminusM; ++i ) {
240  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
241  mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
242  }
243 
244  for( ; i < N-1 ; ++i ) {
245  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
246  mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
247  }
248 
249  y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
250  mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
251 
252  count624 = 0;
253  }
254 
255  y = mt[count624++];
256  y ^= ( y >> 11);
257  y ^= ((y << 7 ) & 0x9d2c5680);
258  y ^= ((y << 15) & 0xefc60000);
259  y ^= ( y >> 18);
260 
261  return (float)(y * twoToMinus_32());
262 }
const int N
Definition: mixmax.h:43
static double twoToMinus_32()
CLHEP::MTwistEngine::operator unsigned int ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 264 of file MTwistEngine.cc.

264  {
265  unsigned int y;
266 
267  if( count624 >= N ) {
268  int i;
269 
270  for( i=0; i < NminusM; ++i ) {
271  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
272  mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
273  }
274 
275  for( ; i < N-1 ; ++i ) {
276  y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
277  mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
278  }
279 
280  y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
281  mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
282 
283  count624 = 0;
284  }
285 
286  y = mt[count624++];
287  y ^= ( y >> 11);
288  y ^= ((y << 7 ) & 0x9d2c5680);
289  y ^= ((y << 15) & 0xefc60000);
290  y ^= ( y >> 18);
291 
292  return y;
293 }
const int N
Definition: mixmax.h:43
std::ostream & CLHEP::MTwistEngine::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 295 of file MTwistEngine.cc.

296 {
297  char beginMarker[] = "MTwistEngine-begin";
298  char endMarker[] = "MTwistEngine-end";
299 
300  int pr = os.precision(20);
301  os << " " << beginMarker << " ";
302  os << theSeed << " ";
303  for (int i=0; i<624; ++i) {
304  os << mt[i] << "\n";
305  }
306  os << count624 << " ";
307  os << endMarker << "\n";
308  os.precision(pr);
309  return os;
310 }
std::vector< unsigned long > CLHEP::MTwistEngine::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 312 of file MTwistEngine.cc.

312  {
313  std::vector<unsigned long> v;
314  v.push_back (engineIDulong<MTwistEngine>());
315  for (int i=0; i<624; ++i) {
316  v.push_back(static_cast<unsigned long>(mt[i]));
317  }
318  v.push_back(count624);
319  return v;
320 }
void CLHEP::MTwistEngine::restoreStatus ( const char  filename[] = "MTwist.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 199 of file MTwistEngine.cc.

200 {
201  std::ifstream inFile( filename, std::ios::in);
202  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
203  std::cerr << " -- Engine state remains unchanged\n";
204  return;
205  }
206 
207  if (!inFile.bad() && !inFile.eof()) {
208  inFile >> theSeed;
209  for (int i=0; i<624; ++i) inFile >> mt[i];
210  inFile >> count624;
211  }
212 }
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
static std::string engineName()
Definition: MTwistEngine.h:77

Here is the call graph for this function:

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

Implements CLHEP::HepRandomEngine.

Definition at line 188 of file MTwistEngine.cc.

189 {
190  std::ofstream outFile( filename, std::ios::out ) ;
191  if (!outFile.bad()) {
192  outFile << theSeed << std::endl;
193  for (int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] << " ";
194  outFile << std::endl;
195  outFile << count624 << std::endl;
196  }
197 }
void CLHEP::MTwistEngine::setSeed ( long  seed,
int  k 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 151 of file MTwistEngine.cc.

151  {
152 
153  // MF 11/15/06 - Change seeding algorithm to a newer one recommended
154  // by Matsumoto: The original algorithm was
155  // mt[i] = (69069 * mt[i-1]) & 0xffffffff and this gives
156  // problems when the seed bit pattern has lots of zeros
157  // (for example, 0x08000000). Savanah bug #17479.
158 
159  theSeed = seed ? seed : 4357;
160  int mti;
161  const int N1=624;
162  mt[0] = (unsigned int) (theSeed&0xffffffffUL);
163  for (mti=1; mti<N1; mti++) {
164  mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
165  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
166  /* In the previous versions, MSBs of the seed affect */
167  /* only MSBs of the array mt[]. */
168  /* 2002/01/09 modified by Makoto Matsumoto */
169  mt[mti] &= 0xffffffffUL;
170  /* for >32 bit machines */
171  }
172  for( int i=1; i < 624; ++i ) {
173  mt[i] ^= k; // MF 9/16/98: distinguish starting points
174  }
175  // MF 11/15/06 This distinction of starting points based on values of k
176  // is kept even though the seeding algorithm itself is improved.
177 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)

Here is the call graph for this function:

Here is the caller graph for this function:

void CLHEP::MTwistEngine::setSeeds ( const long *  seeds,
int  k 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 179 of file MTwistEngine.cc.

179  {
180  setSeed( (*seeds ? *seeds : 43571346), k );
181  int i;
182  for( i=1; i < 624; ++i ) {
183  mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff; // MF 9/16/98
184  }
185  theSeeds = seeds;
186 }
void setSeed(long seed, int)

Here is the call graph for this function:

Here is the caller graph for this function:

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

Implements CLHEP::HepRandomEngine.

Definition at line 214 of file MTwistEngine.cc.

215 {
216  std::cout << std::endl;
217  std::cout << "--------- MTwist engine status ---------" << std::endl;
218  std::cout << std::setprecision(20);
219  std::cout << " Initial seed = " << theSeed << std::endl;
220  std::cout << " Current index = " << count624 << std::endl;
221  std::cout << " Array status mt[] = " << std::endl;
222  // 2014/06/06 L Garren
223  // the final line has 4 elements, not 5
224  for (int i=0; i<620; i+=5) {
225  std::cout << mt[i] << " " << mt[i+1] << " " << mt[i+2] << " "
226  << mt[i+3] << " " << mt[i+4] << "\n";
227  }
228  std::cout << mt[620] << " " << mt[621] << " " << mt[622] << " "
229  << mt[623] << std::endl;
230  std::cout << "----------------------------------------" << std::endl;
231 }

Member Data Documentation

const unsigned int CLHEP::MTwistEngine::VECTOR_STATE_SIZE = 626
static

Definition at line 83 of file MTwistEngine.h.


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