Rand.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. *
  3. * Grabbed by Kevin from http://www.math.keio.ac.jp/~matumoto/cokus.c
  4. * This is the ``Mersenne Twister'' random number generator MT19937, which
  5. * generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
  6. * starting from any odd seed in 0..(2^32 - 1). This version is a recode
  7. * by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by
  8. * Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in
  9. * July-August 1997).
  10. *
  11. * Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha
  12. * running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
  13. * generate 300 million random numbers; after recoding: 24.0 sec. for the same
  14. * (i.e., 46.5% of original time), so speed is now about 12.5 million random
  15. * number generations per second on this machine.
  16. *
  17. * According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html>
  18. * (and paraphrasing a bit in places), the Mersenne Twister is ``designed
  19. * with consideration of the flaws of various existing generators,'' has
  20. * a period of 2^19937 - 1, gives a sequence that is 623-dimensionally
  21. * equidistributed, and ``has passed many stringent tests, including the
  22. * die-hard test of G. Marsaglia and the load test of P. Hellekalek and
  23. * S. Wegenkittl.'' It is efficient in memory usage (typically using 2506
  24. * to 5012 bytes of static data, depending on data type sizes, and the code
  25. * is quite short as well). It generates random numbers in batches of 624
  26. * at a time, so the caching and pipelining of modern systems is exploited.
  27. * It is also divide- and mod-free.
  28. *
  29. * Licensing is free http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/elicense.html
  30. *
  31. * The code as Shawn received it included the following notice:
  32. *
  33. * Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When
  34. * you use this, send an e-mail to <matumoto@math.keio.ac.jp> with
  35. * an appropriate reference to your work.
  36. *
  37. * It would be nice to CC: <Cokus@math.washington.edu> when you write.
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "Rand.h"
  43. //
  44. // uint32 must be an unsigned integer type capable of holding at least 32
  45. // bits; exactly 32 should be fastest, but 64 is better on an Alpha with
  46. // GCC at -O3 optimization so try your options and see what's best for you
  47. //
  48. //typedef unsigned int uint32;
  49. #define N (624) // length of state vector
  50. #define M (397) // a period parameter
  51. #define K (0x9908B0DFU) // a magic constant
  52. #define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u
  53. #define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u
  54. #define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u
  55. #define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v
  56. static unsigned int _state[ N + 1 ]; // state vector + 1 extra to not violate ANSI C
  57. static unsigned int *_next; // next random value is computed from here
  58. static int _left = -1; // can *next++ this many times before reloading
  59. using namespace RakNet;
  60. void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &left );
  61. unsigned int reloadMT( unsigned int *state, unsigned int *&next, int &left );
  62. unsigned int randomMT( unsigned int *state, unsigned int *&next, int &left );
  63. void fillBufferMT( void *buffer, unsigned int bytes, unsigned int *state, unsigned int *&next, int &left );
  64. float frandomMT( unsigned int *state, unsigned int *&next, int &left );
  65. // Uses global vars
  66. void seedMT( unsigned int seed )
  67. {
  68. seedMT(seed, _state, _next, _left);
  69. }
  70. unsigned int reloadMT( void )
  71. {
  72. return reloadMT(_state, _next, _left);
  73. }
  74. unsigned int randomMT( void )
  75. {
  76. return randomMT(_state, _next, _left);
  77. }
  78. float frandomMT( void )
  79. {
  80. return frandomMT(_state, _next, _left);
  81. }
  82. void fillBufferMT( void *buffer, unsigned int bytes )
  83. {
  84. fillBufferMT(buffer, bytes, _state, _next, _left);
  85. }
  86. void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &left ) // Defined in cokus_c.c
  87. {
  88. (void) next;
  89. //
  90. // We initialize state[0..(N-1)] via the generator
  91. //
  92. // x_new = (69069 * x_old) mod 2^32
  93. //
  94. // from Line 15 of Table 1, p. 106, Sec. 3.3.4 of Knuth's
  95. // _The Art of Computer Programming_, Volume 2, 3rd ed.
  96. //
  97. // Notes (SJC): I do not know what the initial state requirements
  98. // of the Mersenne Twister are, but it seems this seeding generator
  99. // could be better. It achieves the maximum period for its modulus
  100. // (2^30) iff x_initial is odd (p. 20-21, Sec. 3.2.1.2, Knuth); if
  101. // x_initial can be even, you have sequences like 0, 0, 0, ...;
  102. // 2^31, 2^31, 2^31, ...; 2^30, 2^30, 2^30, ...; 2^29, 2^29 + 2^31,
  103. // 2^29, 2^29 + 2^31, ..., etc. so I force seed to be odd below.
  104. //
  105. // Even if x_initial is odd, if x_initial is 1 mod 4 then
  106. //
  107. // the lowest bit of x is always 1,
  108. // the next-to-lowest bit of x is always 0,
  109. // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
  110. // the 3rd-from-lowest bit of x 4-cycles ... 0 1 1 0 0 1 1 0 ... ,
  111. // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 0 1 1 1 1 0 ... ,
  112. // ...
  113. //
  114. // and if x_initial is 3 mod 4 then
  115. //
  116. // the lowest bit of x is always 1,
  117. // the next-to-lowest bit of x is always 1,
  118. // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
  119. // the 3rd-from-lowest bit of x 4-cycles ... 0 0 1 1 0 0 1 1 ... ,
  120. // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 1 1 1 1 0 0 ... ,
  121. // ...
  122. //
  123. // The generator's potency (min. s>=0 with (69069-1)^s = 0 mod 2^32) is
  124. // 16, which seems to be alright by p. 25, Sec. 3.2.1.3 of Knuth. It
  125. // also does well in the dimension 2..5 spectral tests, but it could be
  126. // better in dimension 6 (Line 15, Table 1, p. 106, Sec. 3.3.4, Knuth).
  127. //
  128. // Note that the random number user does not see the values generated
  129. // here directly since reloadMT() will always munge them first, so maybe
  130. // none of all of this matters. In fact, the seed values made here could
  131. // even be extra-special desirable if the Mersenne Twister theory says
  132. // so-- that's why the only change I made is to restrict to odd seeds.
  133. //
  134. register unsigned int x = ( seed | 1U ) & 0xFFFFFFFFU, *s = state;
  135. register int j;
  136. for ( left = 0, *s++ = x, j = N; --j;
  137. *s++ = ( x *= 69069U ) & 0xFFFFFFFFU )
  138. ;
  139. }
  140. unsigned int reloadMT( unsigned int *state, unsigned int *&next, int &left )
  141. {
  142. register unsigned int * p0 = state, *p2 = state + 2, *pM = state + M, s0, s1;
  143. register int j;
  144. if ( left < -1 )
  145. seedMT( 4357U );
  146. left = N - 1, next = state + 1;
  147. for ( s0 = state[ 0 ], s1 = state[ 1 ], j = N - M + 1; --j; s0 = s1, s1 = *p2++ )
  148. * p0++ = *pM++ ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
  149. for ( pM = state, j = M; --j; s0 = s1, s1 = *p2++ )
  150. * p0++ = *pM++ ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
  151. s1 = state[ 0 ], *p0 = *pM ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
  152. s1 ^= ( s1 >> 11 );
  153. s1 ^= ( s1 << 7 ) & 0x9D2C5680U;
  154. s1 ^= ( s1 << 15 ) & 0xEFC60000U;
  155. return ( s1 ^ ( s1 >> 18 ) );
  156. }
  157. unsigned int randomMT( unsigned int *state, unsigned int *&next, int &left )
  158. {
  159. unsigned int y;
  160. if ( --left < 0 )
  161. return ( reloadMT(state, next, left) );
  162. y = *next++;
  163. y ^= ( y >> 11 );
  164. y ^= ( y << 7 ) & 0x9D2C5680U;
  165. y ^= ( y << 15 ) & 0xEFC60000U;
  166. return ( y ^ ( y >> 18 ) );
  167. // This change made so the value returned is in the same range as what rand() returns
  168. // return(y ^ (y >> 18)) % 32767;
  169. }
  170. void fillBufferMT( void *buffer, unsigned int bytes, unsigned int *state, unsigned int *&next, int &left )
  171. {
  172. unsigned int offset=0;
  173. unsigned int r;
  174. while (bytes-offset>=sizeof(r))
  175. {
  176. r = randomMT(state, next, left);
  177. memcpy((char*)buffer+offset, &r, sizeof(r));
  178. offset+=sizeof(r);
  179. }
  180. r = randomMT(state, next, left);
  181. memcpy((char*)buffer+offset, &r, bytes-offset);
  182. }
  183. float frandomMT( unsigned int *state, unsigned int *&next, int &left )
  184. {
  185. return ( float ) ( ( double ) randomMT(state, next, left) / 4294967296.0 );
  186. }
  187. RakNetRandom::RakNetRandom()
  188. {
  189. left=-1;
  190. }
  191. RakNetRandom::~RakNetRandom()
  192. {
  193. }
  194. void RakNetRandom::SeedMT( unsigned int seed )
  195. {
  196. printf("%i\n",seed);
  197. seedMT(seed, state, next, left);
  198. }
  199. unsigned int RakNetRandom::ReloadMT( void )
  200. {
  201. return reloadMT(state, next, left);
  202. }
  203. unsigned int RakNetRandom::RandomMT( void )
  204. {
  205. return randomMT(state, next, left);
  206. }
  207. float RakNetRandom::FrandomMT( void )
  208. {
  209. return frandomMT(state, next, left);
  210. }
  211. void RakNetRandom::FillBufferMT( void *buffer, unsigned int bytes )
  212. {
  213. fillBufferMT(buffer, bytes, state, next, left);
  214. }
  215. /*
  216. int main(void)
  217. {
  218. int j;
  219. // you can seed with any uint32, but the best are odds in 0..(2^32 - 1)
  220. seedMT(4357U);
  221. // print the first 2,002 random numbers seven to a line as an example
  222. for(j=0; j<2002; j++)
  223. RAKNET_DEBUG_PRINTF(" %10lu%s", (unsigned int) randomMT(), (j%7)==6 ? "\n" : "");
  224. return(EXIT_SUCCESS);
  225. }
  226. */
粤ICP备19079148号