Author | Message | Time |
---|---|---|
shout | This is code for a mersenne twister thing Ive been doing, and I have a slight problem. It runs correctly twice before resulting in a crash. Using the memory window of VS I can see that it is not overstepping its bounds. I am confused on how it runs twice but then decides that it is going to be problematic. The problem line is: [code] this->m_state[i] = (this->m_state[i + 396]) % 623 ^ (n >> 1); [/code] And the entire thing: [code] MersenneTwister::MersenneTwister(PULONG seed, DWORD length) { this->m_state = new ULONG[this->m_state_len]; memset(this->m_state, 0, this->m_state_len*sizeof(ULONG)); this->m_i = 0; if (seed == NULL || length == 0) { //Now we seed by the return from GetTickCount seed = new ULONG[1]; seed[0] = GetTickCount(); } seed++; for (ULONG i = 0; i < this->m_state_len && i < length; i++) { //It's redundant if the length is 1... but oh well. if (i > length - 1) seed--; this->m_state[i] = seed[i]; } delete [] seed; ULONG n = 0; for (ULONG i = 1; i < 623; i++) { this->m_state[i] = 69069 * m_state[i-1]; } for (ULONG i = 1; i < 623; i++) { n = (this->m_state[i] << 31) + (this->m_state[i-1] >> 1); this->m_state[i] = (this->m_state[i + 397]) % 623 ^ (n >> 1); //Do this if it's odd (?) if (ISODD(this->m_state[i])) this->m_state[i] ^= 0x9908B0DF; } n = (this->m_state[623] << 31) + (this->m_state[0] >> 1); this->m_state[623] = (this->m_state[396]) ^ (n >> 1); if (this->m_state[623] % 2 > 0) this->m_state[623] ^= 0x9908B0DF; }[/code] | April 19, 2006, 5:55 PM |
shout | Weird. When I do this is does not crash until the seventh time. Always the seventh time. It seems to a access violation accessing 'i' when it contains certain numbers after a certain number of iterations. [code] for (ULONG i = 1; i < 441; i++) { n = (this->m_state[i] << 31) + (this->m_state[i-1] >> 1); this->m_state[i] = (this->m_state[i + 397]) % 623 ^ (n >> 1); //Do this if it's odd (?) if (ISODD(this->m_state[i])) this->m_state[i] ^= 0x9908B0DF; } for (ULONG i = 442; i < 623; i++) { n = (this->m_state[i] << 31) + (this->m_state[i-1] >> 1); this->m_state[i] = (this->m_state[i + 397]) % 623 ^ (n >> 1); //Do this if it's odd (?) if (ISODD(this->m_state[i])) this->m_state[i] ^= 0x9908B0DF; }[/code] | April 20, 2006, 5:14 PM |
shout | I figured it out. When I was going at m_state, in this line of code, [code] this->m_state[i] = (this->m_state[i + 397]) % 623 ^ (n >> 1); [/code] I was suppost to be limiting the index of m_state, so it should of been [code] this->m_state[i] = (this->m_state[(i + 397) % 623]) ^ (n >> 1); [/code] | April 20, 2006, 5:30 PM |