Advertisement
yojimbos_law

an incorrect ASH implementation of php's mt_rand()

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