59 const int maxIndex = 215;
62static const int MarkerLen = 64;
69 int numEngines = numberOfEngines++;
70 int cycle = std::abs(
int(numEngines/maxIndex));
71 int curIndex = std::abs(
int(numEngines%maxIndex));
72 long mask = ((cycle & 0x007fffff) << 8);
75 seedlist[0] = (seedlist[0])^mask;
80 for(
int i=0; i < 2000; ++i )
flat();
86 long seedlist[2]={seed,17587};
89 for(
int i=0; i < 2000; ++i )
flat();
95 int cycle = std::abs(
int(rowIndex/maxIndex));
96 int row = std::abs(
int(rowIndex%maxIndex));
97 int col = std::abs(
int(colIndex%2));
98 long mask = (( cycle & 0x000007ff ) << 20 );
101 seedlist[0] = (seedlist[col])^mask;
102 seedlist[1] = 690691;
105 for(
int i=0; i < 2000; ++i )
flat();
119 if( count624 >= N ) {
122 for( i=0; i < NminusM; ++i ) {
123 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
124 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
127 for( ; i < N-1 ; ++i ) {
128 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
129 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
132 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
133 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
140 y ^= ((y << 7 ) & 0x9d2c5680);
141 y ^= ((y << 15) & 0xefc60000);
150 for(
int i=0; i < size; ++i) vect[i] =
flat();
164 mt[0] = (
unsigned int) (
theSeed&0xffffffffUL);
165 for (mti=1; mti<N1; mti++) {
166 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
171 mt[mti] &= 0xffffffffUL;
174 for(
int i=1; i < 624; ++i ) {
182 setSeed( (*seeds ? *seeds : 43571346), k );
184 for( i=1; i < 624; ++i ) {
185 mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff;
192 std::ofstream outFile( filename, std::ios::out ) ;
193 if (!outFile.bad()) {
194 outFile <<
theSeed << std::endl;
195 for (
int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] <<
" ";
196 outFile << std::endl;
197 outFile << count624 << std::endl;
203 std::ifstream inFile( filename, std::ios::in);
205 std::cerr <<
" -- Engine state remains unchanged\n";
209 if (!inFile.bad() && !inFile.eof()) {
211 for (
int i=0; i<624; ++i) inFile >> mt[i];
218 std::cout << std::endl;
219 std::cout <<
"--------- MTwist engine status ---------" << std::endl;
220 std::cout << std::setprecision(20);
221 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
222 std::cout <<
" Current index = " << count624 << std::endl;
223 std::cout <<
" Array status mt[] = " << std::endl;
226 for (
int i=0; i<620; i+=5) {
227 std::cout << mt[i] <<
" " << mt[i+1] <<
" " << mt[i+2] <<
" "
228 << mt[i+3] <<
" " << mt[i+4] <<
"\n";
230 std::cout << mt[620] <<
" " << mt[621] <<
" " << mt[622] <<
" "
231 << mt[623] << std::endl;
232 std::cout <<
"----------------------------------------" << std::endl;
235MTwistEngine::operator double() {
239MTwistEngine::operator float() {
242 if( count624 >= N ) {
245 for( i=0; i < NminusM; ++i ) {
246 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
247 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
250 for( ; i < N-1 ; ++i ) {
251 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
252 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
255 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
256 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
263 y ^= ((y << 7 ) & 0x9d2c5680);
264 y ^= ((y << 15) & 0xefc60000);
267 return (
float)(y * twoToMinus_32());
270MTwistEngine::operator
unsigned int() {
273 if( count624 >= N ) {
276 for( i=0; i < NminusM; ++i ) {
277 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
278 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
281 for( ; i < N-1 ; ++i ) {
282 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
283 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
286 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
287 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0x1) ? 0x9908b0df : 0x0 );
294 y ^= ((y << 7 ) & 0x9d2c5680);
295 y ^= ((y << 15) & 0xefc60000);
303 char beginMarker[] =
"MTwistEngine-begin";
304 char endMarker[] =
"MTwistEngine-end";
306 int pr = os.precision(20);
307 os <<
" " << beginMarker <<
" ";
309 for (
int i=0; i<624; ++i) {
312 os << count624 <<
" ";
313 os << endMarker <<
"\n";
319 std::vector<unsigned long> v;
320 v.push_back (engineIDulong<MTwistEngine>());
321 for (
int i=0; i<624; ++i) {
322 v.push_back(
static_cast<unsigned long>(mt[i]));
324 v.push_back(count624);
330 char beginMarker [MarkerLen];
336 if (strcmp(beginMarker,
"MTwistEngine-begin")) {
337 is.clear(std::ios::badbit | is.rdstate());
338 std::cerr <<
"\nInput stream mispositioned or"
339 <<
"\nMTwistEngine state description missing or"
340 <<
"\nwrong engine type found." << std::endl;
347 return "MTwistEngine-begin";
352 char endMarker [MarkerLen];
354 for (
int i=0; i<624; ++i) is >> mt[i];
359 if (strcmp(endMarker,
"MTwistEngine-end")) {
360 is.clear(std::ios::badbit | is.rdstate());
361 std::cerr <<
"\nMTwistEngine state description incomplete."
362 <<
"\nInput stream is probably mispositioned now." << std::endl;
369 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
371 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
380 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
383 for (
int i=0; i<624; ++i) {
#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()