56 const int maxIndex = 215;
66 int numEngines = numberOfEngines++;
67 int cycle =
std::abs(
int(numEngines/maxIndex));
68 int curIndex =
std::abs(
int(numEngines%maxIndex));
69 long mask = ((cycle & 0x007fffff) << 8);
72 seedlist[0] = (seedlist[0])^mask;
77 for(
int i=0; i < 2000; ++i )
flat();
83 long seedlist[2]={
seed,17587};
86 for(
int i=0; i < 2000; ++i )
flat();
92 int cycle =
std::abs(
int(rowIndex/maxIndex));
93 int row =
std::abs(
int(rowIndex%maxIndex));
95 long mask = (( cycle & 0x000007ff ) << 20 );
98 seedlist[0] = (seedlist[
col])^mask;
102 for(
int i=0; i < 2000; ++i )
flat();
120 y = (
mt[i] & 0x80000000) | (
mt[i+1] & 0x7fffffff);
121 mt[i] =
mt[i+
M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
124 for( ; i <
N-1 ; ++i ) {
125 y = (
mt[i] & 0x80000000) | (
mt[i+1] & 0x7fffffff);
126 mt[i] =
mt[i-
NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
129 y = (
mt[i] & 0x80000000) | (
mt[0] & 0x7fffffff);
130 mt[i] =
mt[
M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
137 y ^= ((y << 7 ) & 0x9d2c5680);
138 y ^= ((y << 15) & 0xefc60000);
147 for(
int i=0; i < size; ++i) vect[i] =
flat();
162 for (mti=1; mti<N1; mti++) {
163 mt[mti] = (1812433253UL * (
mt[mti-1] ^ (
mt[mti-1] >> 30)) + mti);
168 mt[mti] &= 0xffffffffUL;
171 for(
int i=1; i < 624; ++i ) {
179 setSeed( (*seeds ? *seeds : 43571346), k );
181 for( i=1; i < 624; ++i ) {
182 mt[i] = ( seeds[1] +
mt[i] ) & 0xffffffff;
189 std::ofstream outFile( filename, std::ios::out ) ;
190 if (!outFile.bad()) {
191 outFile <<
theSeed << std::endl;
192 for (
int i=0; i<624; ++i) outFile <<std::setprecision(20) <<
mt[i] <<
" ";
193 outFile << std::endl;
202 std::cerr <<
" -- Engine state remains unchanged\n";
206 if (!inFile.bad() && !inFile.eof()) {
208 for (
int i=0; i<624; ++i) inFile >>
mt[i];
215 std::cout << std::endl;
216 std::cout <<
"--------- MTwist engine status ---------" << std::endl;
217 std::cout << std::setprecision(20);
218 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
219 std::cout <<
" Current index = " <<
count624 << std::endl;
220 std::cout <<
" Array status mt[] = " << std::endl;
223 for (
int i=0; i<620; i+=5) {
224 std::cout <<
mt[i] <<
" " <<
mt[i+1] <<
" " <<
mt[i+2] <<
" "
225 <<
mt[i+3] <<
" " <<
mt[i+4] <<
"\n";
227 std::cout <<
mt[620] <<
" " <<
mt[621] <<
" " <<
mt[622] <<
" "
228 <<
mt[623] << std::endl;
229 std::cout <<
"----------------------------------------" << std::endl;
232 MTwistEngine::operator double() {
236 MTwistEngine::operator float() {
239 if( count624 >=
N ) {
242 for( i=0; i < NminusM; ++i ) {
243 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
244 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
247 for( ; i <
N-1 ; ++i ) {
248 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
249 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
252 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
253 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
260 y ^= ((y << 7 ) & 0x9d2c5680);
261 y ^= ((y << 15) & 0xefc60000);
264 return (
float)(y * twoToMinus_32());
267 MTwistEngine::operator
unsigned int() {
270 if( count624 >=
N ) {
273 for( i=0; i < NminusM; ++i ) {
274 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
275 mt[i] = mt[i+M] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
278 for( ; i <
N-1 ; ++i ) {
279 y = (mt[i] & 0x80000000) | (mt[i+1] & 0x7fffffff);
280 mt[i] = mt[i-NminusM] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
283 y = (mt[i] & 0x80000000) | (mt[0] & 0x7fffffff);
284 mt[i] = mt[M-1] ^ (y >> 1) ^ ((y & 0
x1) ? 0x9908b0df : 0x0 );
291 y ^= ((y << 7 ) & 0x9d2c5680);
292 y ^= ((y << 15) & 0xefc60000);
300 char beginMarker[] =
"MTwistEngine-begin";
301 char endMarker[] =
"MTwistEngine-end";
303 int pr = os.precision(20);
304 os <<
" " << beginMarker <<
" ";
306 for (
int i=0; i<624; ++i) {
310 os << endMarker <<
"\n";
316 std::vector<unsigned long>
v;
317 v.push_back (engineIDulong<MTwistEngine>());
318 for (
int i=0; i<624; ++i) {
319 v.push_back(static_cast<unsigned long>(
mt[i]));
333 if (strcmp(beginMarker,
"MTwistEngine-begin")) {
334 is.clear(std::ios::badbit | is.rdstate());
335 std::cerr <<
"\nInput stream mispositioned or"
336 <<
"\nMTwistEngine state description missing or"
337 <<
"\nwrong engine type found." << std::endl;
344 return "MTwistEngine-begin";
351 for (
int i=0; i<624; ++i) is >>
mt[i];
356 if (strcmp(endMarker,
"MTwistEngine-end")) {
357 is.clear(std::ios::badbit | is.rdstate());
358 std::cerr <<
"\nMTwistEngine state description incomplete."
359 <<
"\nInput stream is probably mispositioned now." << std::endl;
366 if ((v[0] & 0xffffffffUL) != engineIDulong<MTwistEngine>()) {
368 "\nMTwistEngine get:state vector has wrong ID word - state unchanged\n";
377 "\nMTwistEngine get:state vector has wrong length - state unchanged\n";
380 for (
int i=0; i<624; ++i) {