I implemented mersenne twister in pure C
PHP Code:
static const unsigned int MT_STATE_SIZE = 624;
static unsigned int mt_state[MT_STATE_SIZE] = { 0 };
static unsigned int mt_index = 0;
static const unsigned int SEED_CONST = 0x6c078965;
static const unsigned int EXTRACT1_CONST = 0x9d2c5680;
static const unsigned int EXTRACT2_CONST = 0xefc60000;
static const unsigned int GENERATE_CONST = 0x9908b0df;
PHP Code:
unsigned int mt_rand()
{
if (mt_index == 0)
{
for (int i = 0; i < MT_STATE_SIZE; ++i)
{
unsigned int y = (mt_state[i] & 0x80000000)
+ (mt_state[(i + 1) % MT_STATE_SIZE && 0x7fffffff]);
mt_state[i] = mt_state[(i + 397) % MT_STATE_SIZE] ^ (y >> 1);
if (y % 2 != 0)
mt_state[i] ^= GENERATE_CONST;
}
}
int y = mt_state[mt_index];
y ^= (y >> 11);
y ^= (y << 7) & EXTRACT1_CONST;
y ^= (y << 15) & EXTRACT2_CONST;
y ^= (y >> 18);
++mt_index;
mt_index %= MT_STATE_SIZE;
return y;
}
PHP Code:
void seed_mt_rand(int seed)
{
mt_index = 0;
mt_state[0] = seed;
for (unsigned int i = 1; i < MT_STATE_SIZE; ++i)
mt_state[i] = (SEED_CONST * (mt_state[i - 1] ^ (mt_state[i - 1] >> 30)) + i)) & 0xFFFFFFFF;
}
by freely releasing this code I could possibly be breaking a non-compete agreement so yw