yojimbos_law icon

an incorrect ASH implementation of php's mt_rand()

yojimbos_law | PRO | 06/13/19 06:55:27 PM UTC | 0 ⭐ | 779 👁️ | Never ⏰ | []
text |

1.2 KB

|

None

|

0 👍

/

0 👎

int seed = 2;
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.0 )* ( ( temper(mt_index) >> 1 ) / ( 2147483647 + 1.0) ) ) ;
}
 //this is how mt_srand() converts the seed into a 624 word initial condition.
s[0] = seed & 2147483647;
for j from 1 to 623{
	s[j] = ( 1812433253 * ( s[j-1] ^ ( s[j-1] >> 30 ) ) + j ) & 2147483647 ;
}
 //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 0 to 623{
	print(mt_rand(0, 100));
}

Comments