Gassa

random.h

Dec 15th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. /* Random from Gassa */
  2. /* Updated by Burunduk1 and Burunduk3 */
  3.  
  4. /** please notice all changed in this log **
  5. * 2009-08-15 - rndLong() was added
  6. * 2010-11-29 - asserts are added, comments are modified [Burunduk1]
  7. * 2010-11-29 - rndLong is fixed, R(ll, ll) is added [Burunduk1]
  8. * 2011-06-12 - bug in rndLong if fixed [Burunduk1]
  9. * 2013-02-15 - bug in Time() under MSVC -- do not use "low", this word is reserved
  10. ****/
  11.  
  12. #ifndef __random_h__
  13. #define __random_h__
  14.  
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <cassert>
  19. #include <algorithm>
  20.  
  21. /***
  22. * Declarations
  23. ***/
  24.  
  25. typedef unsigned uint;
  26. typedef long long ll;
  27. typedef unsigned long long ull;
  28. typedef long double dbl;
  29.  
  30. #define rndvalue rndInt
  31.  
  32. int nextrand(); // random in [0..MBIG)
  33. void initrand( int seed ); // initialize random
  34. dbl rndDouble(); // random in [0..1)
  35. int rndInt( int k ); // random in [0..k)
  36. ll rndLong( ll k ); // random in [0..k) version for "long long"
  37. int R( int A, int B ); // random in [A..B]
  38. ll R( ll A, ll B ); // random in [A..B]
  39. ll Time(); // returns number of tics since processor was turned on
  40. template <typename Type>
  41. void randomShuffle( Type start, Type finish ); // shuffles an array like std::random_shuffle, but using our random
  42.  
  43. /***
  44. * Definitions
  45. ***/
  46.  
  47. const int MBIG = (int)1e9, MSEED = 161803398;
  48. const double FAC = 1.0L / MBIG;
  49.  
  50. int inext, inextp;
  51. int ra [56];
  52.  
  53. int nextrand() // Random in [0..MBIG)
  54. {
  55. int j;
  56. if (++inext == 56)
  57. inext = 1;
  58. if (++inextp == 56)
  59. inextp = 1;
  60. j = ra[inext] - ra[inextp];
  61. if (j < 0)
  62. j += MBIG;
  63. ra[inext] = j;
  64. return j;
  65. }
  66.  
  67. void initrand(int seed)
  68. {
  69. int i, j, k, l;
  70. seed = seed * seed + seed * 239017 + 13;
  71. l = MSEED - seed;
  72. l %= MBIG;
  73. if (l < 0)
  74. l += MBIG;
  75. ra[55] = l;
  76. k = 1;
  77. for (i = 1, j = 0; i <= 54; i++)
  78. {
  79. j += 21;
  80. if (j > 55)
  81. j -= 55;
  82. ra[j] = k;
  83. k = l - k;
  84. if (k < 0)
  85. k += MBIG;
  86. l = ra[j];
  87. }
  88. for (k = 1; k <= 4; k++)
  89. for (i = 1, j = 31; i <= 55; i++, j >= 55 ? j = 1 : j++)
  90. {
  91. ra[i] -= ra[j];
  92. if (ra[i] < 0)
  93. ra[i] += MBIG;
  94. }
  95. inext = 0;
  96. inextp = 31;
  97. }
  98.  
  99. dbl rndDouble() // Random in [0..1)
  100. {
  101. return nextrand () * FAC;
  102. }
  103.  
  104. int rndInt(int k) // Random in [0..k)
  105. {
  106. assert(1 <= k);
  107. return (int)(rndDouble () * k);
  108. }
  109.  
  110. ll rndLong( ll k ) // Random in [0..x)
  111. {
  112. assert(1 <= k);
  113. return (ll)((rndDouble() * FAC + rndDouble()) * k);
  114. }
  115.  
  116. int R( int A, int B )
  117. {
  118. assert(A <= B && (uint)B - A < (1LL << 31) - 1);
  119. return A + rndInt(B - A + 1);
  120. }
  121.  
  122. ll R( ll A, ll B )
  123. {
  124. assert(A <= B && (ull)B - A < (1ULL << 63) - 1);
  125. return A + rndLong(B - A + 1);
  126. }
  127.  
  128. ll Time()
  129. {
  130. #ifdef __GNUC__
  131. ll res;
  132. asm volatile ("rdtsc" : "=A" (res));
  133. return res;
  134. #else
  135. int loww, hi;
  136. __asm
  137. {
  138. rdtsc
  139. mov loww, eax
  140. mov hi, edx
  141. }
  142. return ((ll)hi << 32) | loww;
  143. #endif
  144. }
  145.  
  146. template <typename Type>
  147. void randomShuffle( Type start, Type finish )
  148. {
  149. for (int i = 0; start + i != finish; i++)
  150. {
  151. int j = R(0, i);
  152. std::swap(*(start + i), *(start + j));
  153. }
  154. }
  155.  
  156. #endif // __random_h__
Advertisement
Add Comment
Please, Sign In to add comment