Advertisement
yojimbos_law

ASH implementation of PHP 5.3's seeded mt_rand()

Jun 15th, 2019
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. //returns the [position]-th number that mt_rand([low],[high]) would return in PHP 5.3 when seeded with [seed]
  2. int seeded_mt_rand(int seed,int low, int high, int position){
  3. int sd = seed;
  4. int[int] s;
  5. int mt_index = 623;
  6.  
  7. //this is the MT recursion function, maybe?
  8. int twist(int m, int u,int v){
  9. return ( m ^ ( ( ( u & 2147483648 ) | ( v & 2147483647 ) ) >> 1 ) ^ ( ( 4294967295 * ( u & 1 ) ) & 2567483615 ) ) ;
  10. }
  11.  
  12. //this is the tempering function. God only knows what it does.
  13. int temper(int x){
  14. int s_one;
  15. s_one = s[x];
  16. s_one ^= (s_one >> 11);
  17. s_one ^= (s_one << 7) & 2636928640;
  18. s_one ^= (s_one << 15) & 4022730752;
  19. return ( s_one ^ (s_one >> 18) );
  20. }
  21.  
  22.  
  23. //this is going to actually spit out random numbers in the specified range, allegedly.
  24. int mt_rand(int min, int max){
  25. mt_index++;
  26. return (min + ( max - min + 1 ) * (( temper(mt_index) >> 1 ) / ( 2147483647 + 1.0))) ;
  27. }
  28.  
  29. //this is how mt_srand() converts the seed into a 624 word initial condition.
  30. s[0] = sd & 2147483647;
  31.  
  32. for j from 1 to 623{
  33. s[j] = ( ( 1812433253 * ( s[j-1] ^ ( s[j-1] >> 30 ) )) + j ) & 4294967295 ;
  34. }
  35.  
  36. //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?
  37. for i from 0 to 623{
  38. s[624+i] = twist(s[397+i],s[i],s[i+1]);
  39. }
  40.  
  41. for i from 1 to position-1{
  42. mt_rand(low, high);
  43. }
  44. return(mt_rand(low, high));
  45. }
  46.  
  47.  
  48. //prints the values of the third and second mt_rand(0,100) calls after mt_srand(2).
  49. print(seeded_mt_rand(2,0,100,3));
  50. print(seeded_mt_rand(2,0,100,2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement