Advertisement
yojimbos_law

revised and still bad ASH implementation of php's mt_rand()

Jun 15th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. int seed = 2;
  2. int[int] s;
  3. int mt_index = 623;
  4.  
  5. //this is a function for 32bit multiplication because ASH doesn't manipulate numbers the way we need.
  6.  
  7. int mult32(int a, int b) {
  8. int product = 0;
  9. for (int i = 0; i < 32; i++) {
  10. product += (a << i) * (1 & b);
  11. b >>>= 1;
  12. }
  13. return product;
  14. }
  15.  
  16. //this is some linear map that we use in the recursion function.
  17. //except we don't actually use it because php made an oopsie where they look at the lowest bit of the wrong thing.
  18. int A(int x){
  19. if((x&1) == 0){
  20. return (x >>> 1);
  21. }
  22. else{
  23. return ((x >>> 1) ^ 2567483615);
  24. }
  25. }
  26.  
  27. //this is the MT recursion function, maybe?
  28. int twist(int m, int u,int v){
  29. //if done correctly(?):
  30. //return (m ^ A( ( ( u & 2147483648 ) | ( v & 2147483647 ) ) ));
  31.  
  32. //the way php does it:
  33. return ( m ^ ( ( ( u & 2147483648 ) | ( v & 2147483647 ) ) >> 1 ) ^ ( ( 4294967295 * ( u & 1 ) ) & 2567483615 ) ) ;
  34. }
  35.  
  36. //this is the tempering function. God only knows what it does.
  37. int temper(int x){
  38. int s_one;
  39. s_one = s[x];
  40. s_one ^= (s_one >> 11);
  41. //s_one ^= ((s_one << 7)%4294967295) & 2636928640;
  42. //s_one ^= ((s_one << 15)%4294967295) & 4022730752;
  43. s_one ^= (s_one << 7) & 2636928640;
  44. s_one ^= (s_one << 15) & 4022730752;
  45. //s_one ^= (((s_one << 7)<<32)>>>32) & 2636928640;
  46. //s_one ^= (((s_one << 15)<<)>>32) & 4022730752;
  47. return ( s_one ^ (s_one >> 18) );
  48. }
  49.  
  50.  
  51. //this is going to actually spit out random numbers in the specified range, allegedly.
  52. int mt_rand(int min, int max){
  53. mt_index++;
  54. return (min + ( max - min + 1 ) * (( temper(mt_index) >> 1 ) / ( 2147483647 + 1.0))) ;
  55. }
  56.  
  57. //this is how mt_srand() converts the seed into a 624 word initial condition.
  58. s[0] = seed & 2147483647;
  59.  
  60. for j from 1 to 623{
  61. //s[j] = ( ( ( 1812433253 * ( s[j-1] ^ ( s[j-1] >> 30 ) ) ) + j ) % 4294967295 ) & 2147483647 ;
  62. s[j] = ( ( mult32( 1812433253 , ( s[j-1] ^ ( s[j-1] >>> 30 ) ) ) + j ) ) & 2147483647 ;
  63. //s[j] = ( ( 1812433253 * ( s[j-1] ^ ( s[j-1] >> 30 ) )) + j ) & 2147483647 ;
  64. //print(s[j]);
  65. }
  66.  
  67. //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?
  68. for i from 0 to 623{
  69. s[624+i] = twist(s[397+i],s[i],s[i+1]);
  70. }
  71.  
  72. for i from 0 to 10{
  73. print(mt_rand(0, 100));
  74. }
  75.  
  76. /*
  77. output:
  78.  
  79. 29
  80. 66
  81. 51
  82. 13
  83. 83
  84. 55
  85. 4
  86. 27
  87. 20
  88. 22
  89. 2
  90. */
  91.  
  92. /*
  93. output upon commenting out line 62 and uncommenting 61:
  94. 27
  95. 64
  96. 48
  97. 58
  98. 78
  99. 83
  100. 67
  101. 48
  102. 4
  103. 72
  104. 42
  105.  
  106. and all such combinations of comments/uncomments produce one of those two lists.
  107. */
  108. /*
  109. here's what some php sandbox says it should be: https://imgur.com/8ta2ZmG
  110. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement