//returns the [position]-th number that mt_rand([low],[high]) would return in PHP 5.3 when seeded with [seed]
int seeded_mt_rand(int seed,int low, int high, int position){
int sd = seed;
int[int] s;
int mt_index = 623;
//this is the MT recursion function, maybe?
int twist(int m, int u,int v){
return ( m ^ ( ( ( u & 2147483648 ) | ( v & 2147483647 ) ) >> 1 ) ^ ( ( 4294967295 * ( u & 1 ) ) & 2567483615 ) ) ;
}
//this is the tempering function. God only knows what it does.
int temper(int x){
int s_one;
s_one = s[x];
s_one ^= (s_one >> 11);
s_one ^= (s_one << 7) & 2636928640;
s_one ^= (s_one << 15) & 4022730752;
return ( s_one ^ (s_one >> 18) );
}
//this is going to actually spit out random numbers in the specified range, allegedly.
int mt_rand(int min, int max){
mt_index++;
return (min + ( max - min + 1 ) * (( temper(mt_index) >> 1 ) / ( 2147483647 + 1.0))) ;
}
//this is how mt_srand() converts the seed into a 624 word initial condition.
s[0] = sd & 2147483647;
for j from 1 to 623{
s[j] = ( ( 1812433253 * ( s[j-1] ^ ( s[j-1] >> 30 ) )) + j ) & 4294967295 ;
}
//this generates the next 624 terms, which are the first 624 outputs of mt_rand() before tempering, probably? and I don't care about more than that, probably?
for i from 0 to 623{
s[624+i] = twist(s[397+i],s[i],s[i+1]);
}
for i from 1 to position-1{
mt_rand(low, high);
}
return(mt_rand(low, high));
}
//prints the values of the third and second mt_rand(0,100) calls after mt_srand(2).
print(seeded_mt_rand(2,0,100,3));
print(seeded_mt_rand(2,0,100,2));
Comments