41#include "CLHEP/Random/defs.h"
42#include "CLHEP/Random/Random.h"
43#include "CLHEP/Random/MTwistEngine.h"
44#include "CLHEP/Random/engineIDulong.h"
45#include "CLHEP/Utility/atomic_int.h"
60 const int maxIndex = 215;
63static const int MarkerLen = 64;
70 int numEngines = numberOfEngines++;
71 int cycle = std::abs(
int(numEngines/maxIndex));
72 int curIndex = std::abs(
int(numEngines%maxIndex));
73 long mask = ((cycle & 0x007fffff) << 8);
76 seedlist[0] = (seedlist[0])^mask;
81 for(
int i=0; i < 2000; ++i )
flat();
87 long seedlist[2]={seed,17587};
90 for(
int i=0; i < 2000; ++i )
flat();
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 );
102 seedlist[0] = (seedlist[col])^mask;
103 seedlist[1] = 690691;
106 for(
int i=0; i < 2000; ++i )
flat();
120 if( count624 >= N ) {
123 for( i=0; i < NminusM; ++i ) {
124 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
125 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
128 for( ; i < N-1 ; ++i ) {
129 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
130 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
133 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
134 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
141 y ^= ((y << 7 ) & 0x9d2c5680);
142 y ^= ((y << 15) & 0xefc60000);
151 for(
int i=0; i < size; ++i) vect[i] =
flat();
165 mt[0] = (
unsigned int) (
theSeed&0xffffffffUL);
166 for (mti=1; mti<N1; mti++) {
167 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
172 mt[mti] &= 0xffffffffUL;
175 for(
int i=1; i < 624; ++i ) {
183 setSeed( (*seeds ? *seeds : 43571346), k );
185 for( i=1; i < 624; ++i ) {
186 mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff;
193 std::ofstream outFile( filename, std::ios::out ) ;
194 if (!outFile.bad()) {
195 outFile <<
theSeed << std::endl;
196 for (
int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] <<
" ";
197 outFile << std::endl;
198 outFile << count624 << std::endl;
204 std::ifstream inFile( filename, std::ios::in);
206 std::cerr <<
" -- Engine state remains unchanged\n";
210 if (!inFile.bad() && !inFile.eof()) {
212 for (
int i=0; i<624; ++i) inFile >> mt[i];
219 std::cout << std::endl;
220 std::cout <<
"--------- MTwist engine status ---------" << std::endl;
221 std::cout << std::setprecision(20);
222 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
223 std::cout <<
" Current index = " << count624 << std::endl;
224 std::cout <<
" Array status mt[] = " << std::endl;
227 for (
int i=0; i<620; i+=5) {
228 std::cout << mt[i] <<
" " << mt[i+1] <<
" " << mt[i+2] <<
" "
229 << mt[i+3] <<
" " << mt[i+4] <<
"\n";
231 std::cout << mt[620] <<
" " << mt[621] <<
" " << mt[622] <<
" "
232 << mt[623] << std::endl;
233 std::cout <<
"----------------------------------------" << std::endl;
240MTwistEngine::operator float() {
243 if( count624 >= N ) {
246 for( i=0; i < NminusM; ++i ) {
247 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
248 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
251 for( ; i < N-1 ; ++i ) {
252 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
253 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
256 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
257 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
264 y ^= ((y << 7 ) & 0x9d2c5680);
265 y ^= ((y << 15) & 0xefc60000);
268 return (
float)(y * twoToMinus_32());
271MTwistEngine::operator
unsigned int() {
274 if( count624 >= N ) {
277 for( i=0; i < NminusM; ++i ) {
278 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
279 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
282 for( ; i < N-1 ; ++i ) {
283 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
284 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
287 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
288 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
295 y ^= ((y << 7 ) & 0x9d2c5680);
296 y ^= ((y << 15) & 0xefc60000);
304 char beginMarker[] =
"MTwistEngine-begin";
305 char endMarker[] =
"MTwistEngine-end";
307 long pr = os.precision(20);
308 os <<
" " << beginMarker <<
" ";
310 for (
int i=0; i<624; ++i) {
313 os << count624 <<
" ";
314 os << endMarker <<
"\n";
320 std::vector<unsigned long> v;
321 v.push_back (engineIDulong<MTwistEngine>());
322 for (
int i=0; i<624; ++i) {
323 v.push_back(
static_cast<unsigned long>(mt[i]));
325 v.push_back(count624);
331 char beginMarker [MarkerLen];
337 if (strcmp(beginMarker,
"MTwistEngine-begin")) {
338 is.clear(std::ios::badbit | is.rdstate());
339 std::cerr <<
"\nInput stream mispositioned or"
340 <<
"\nMTwistEngine state description missing or"
341 <<
"\nwrong engine type found." << std::endl;
348 return "MTwistEngine-begin";
353 char endMarker [MarkerLen];
355 for (
int i=0; i<624; ++i) is >> mt[i];
360 if (strcmp(endMarker,
"MTwistEngine-end")) {
361 is.clear(std::ios::badbit | is.rdstate());
362 std::cerr <<
"\nMTwistEngine state description incomplete."
363 <<
"\nInput stream is probably mispositioned now." << std::endl;
370 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
372 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
381 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
384 for (
int i=0; i<624; ++i) {
385 mt[i]=(
unsigned int)v[i+1];
387 count624 = (int)v[625];
#define CLHEP_ATOMIC_INT_TYPE
static double twoToMinus_32()
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)
static void getTheTableSeeds(long *seeds, int index)
virtual std::istream & get(std::istream &is)
virtual std::istream & getState(std::istream &is)
void flatArray(const int size, double *vect)
void restoreStatus(const char filename[]="MTwist.conf")
static std::string engineName()
void setSeeds(const long *seeds, int)
void saveStatus(const char filename[]="MTwist.conf") const
std::vector< unsigned long > put() const
static const unsigned int VECTOR_STATE_SIZE
void setSeed(long seed, int)
static std::string beginTag()