50 static const int MarkerLen = 64;
54 int MTwistEngine::numEngines = 0;
55 int MTwistEngine::maxIndex = 215;
60 int cycle = std::abs(
int(numEngines/maxIndex));
61 int curIndex = std::abs(
int(numEngines%maxIndex));
62 long mask = ((cycle & 0x007fffff) << 8);
65 seedlist[0] = (seedlist[0])^mask;
70 for(
int i=0; i < 2000; ++i )
flat();
76 long seedlist[2]={seed,17587};
79 for(
int i=0; i < 2000; ++i )
flat();
85 int cycle = std::abs(
int(rowIndex/maxIndex));
86 int row = std::abs(
int(rowIndex%maxIndex));
87 int col = std::abs(
int(colIndex%2));
88 long mask = (( cycle & 0x000007ff ) << 20 );
91 seedlist[0] = (seedlist[
col])^mask;
95 for(
int i=0; i < 2000; ++i )
flat();
109 if( count624 >= N ) {
112 for( i=0; i < NminusM; ++i ) {
113 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
114 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
117 for( ; i < N-1 ; ++i ) {
118 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
119 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
122 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
123 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
130 y ^= ((y << 7 ) & 0x9d2c5680);
131 y ^= ((y << 15) & 0xefc60000);
140 for(
int i=0; i < size; ++i) vect[i] =
flat();
154 mt[0] = (
unsigned int) (
theSeed&0xffffffffUL);
155 for (mti=1; mti<N1; mti++) {
156 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
161 mt[mti] &= 0xffffffffUL;
164 for(
int i=1; i < 624; ++i ) {
172 setSeed( (*seeds ? *seeds : 43571346), k );
174 for( i=1; i < 624; ++i ) {
175 mt[i] = ( seeds[1] + mt[i] ) & 0xffffffff;
182 std::ofstream
outFile( filename, std::ios::out ) ;
183 if (!outFile.bad()) {
184 outFile <<
theSeed << std::endl;
185 for (
int i=0; i<624; ++i) outFile <<std::setprecision(20) << mt[i] <<
" ";
186 outFile << std::endl;
187 outFile << count624 << std::endl;
195 std::cerr <<
" -- Engine state remains unchanged\n";
199 if (!inFile.bad() && !inFile.eof()) {
201 for (
int i=0; i<624; ++i) inFile >> mt[i];
208 std::cout << std::endl;
209 std::cout <<
"--------- MTwist engine status ---------" << std::endl;
210 std::cout << std::setprecision(20);
211 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
212 std::cout <<
" Current index = " << count624 << std::endl;
213 std::cout <<
" Array status mt[] = " << std::endl;
214 for (
int i=0; i<624; i+=5) {
215 std::cout << mt[i] <<
" " << mt[i+1] <<
" " << mt[i+2] <<
" "
216 << mt[i+3] <<
" " << mt[i+4] << std::endl;
218 std::cout <<
"----------------------------------------" << std::endl;
221 MTwistEngine::operator float() {
224 if( count624 >=
N ) {
227 for( i=0; i < NminusM; ++i ) {
228 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
229 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
232 for( ; i <
N-1 ; ++i ) {
233 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
234 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
237 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
238 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
245 y ^= ((y << 7 ) & 0x9d2c5680);
246 y ^= ((y << 15) & 0xefc60000);
249 return (
float)(y * twoToMinus_32());
252 MTwistEngine::operator
unsigned int() {
255 if( count624 >=
N ) {
258 for( i=0; i < NminusM; ++i ) {
259 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
260 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
263 for( ; i <
N-1 ; ++i ) {
264 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
265 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
268 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
269 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
276 y ^= ((y << 7 ) & 0x9d2c5680);
277 y ^= ((y << 15) & 0xefc60000);
285 char beginMarker[] =
"MTwistEngine-begin";
286 char endMarker[] =
"MTwistEngine-end";
288 int pr = os.precision(20);
289 os <<
" " << beginMarker <<
" ";
291 for (
int i=0; i<624; ++i) {
294 os << count624 <<
" ";
295 os << endMarker <<
"\n";
301 std::vector<unsigned long>
v;
302 v.push_back (engineIDulong<MTwistEngine>());
303 for (
int i=0; i<624; ++i) {
304 v.push_back(static_cast<unsigned long>(mt[i]));
306 v.push_back(count624);
312 char beginMarker [MarkerLen];
318 if (strcmp(beginMarker,
"MTwistEngine-begin")) {
319 is.clear(std::ios::badbit | is.rdstate());
320 std::cerr <<
"\nInput stream mispositioned or"
321 <<
"\nMTwistEngine state description missing or"
322 <<
"\nwrong engine type found." << std::endl;
329 return "MTwistEngine-begin";
334 char endMarker [MarkerLen];
336 for (
int i=0; i<624; ++i) is >> mt[i];
341 if (strcmp(endMarker,
"MTwistEngine-end")) {
342 is.clear(std::ios::badbit | is.rdstate());
343 std::cerr <<
"\nMTwistEngine state description incomplete."
344 <<
"\nInput stream is probably mispositioned now." << std::endl;
351 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
353 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
362 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
365 for (
int i=0; i<624; ++i) {
static double nearlyTwoToMinus_54()
void saveStatus(const char filename[]="MTwist.conf") const
void setSeeds(const long *seeds, int)
virtual std::istream & get(std::istream &is)
void setSeed(long seed, int)
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
static double twoToMinus_32()
void restoreStatus(const char filename[]="MTwist.conf")
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static const unsigned int VECTOR_STATE_SIZE
static double twoToMinus_53()
static std::string beginTag()
void flatArray(const int size, double *vect)
virtual std::istream & getState(std::istream &is)
static std::string engineName()
std::vector< unsigned long > put() const
static void getTheTableSeeds(long *seeds, int index)