PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mt.c

https://github.com/isaac/MacRuby
C | 178 lines | 88 code | 22 blank | 68 comment | 11 complexity | 57e0f23f609c033c25ec0f12a0dd8170 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * This is based on trimmed version of MT19937. To get the original version,
  3. * contact <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
  4. *
  5. * The original copyright notice follows.
  6. *
  7. * A C-program for MT19937, with initialization improved 2002/2/10.
  8. * Coded by Takuji Nishimura and Makoto Matsumoto.
  9. * This is a faster version by taking Shawn Cokus's optimization,
  10. * Matthe Bellew's simplification, Isaku Wada's real version.
  11. *
  12. * Before using, initialize the state by using init_genrand(mt, seed)
  13. * or init_by_array(mt, init_key, key_length).
  14. *
  15. * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * 3. The names of its contributors may not be used to endorse or promote
  30. * products derived from this software without specific prior written
  31. * permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  40. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  41. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  42. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. *
  46. * Any feedback is very welcome.
  47. * http://www.math.keio.ac.jp/matumoto/emt.html
  48. * email: matumoto@math.keio.ac.jp
  49. */
  50. #include <limits.h>
  51. typedef int int_must_be_32bit_at_least[sizeof(int) * CHAR_BIT < 32 ? -1 : 1];
  52. /* Period parameters */
  53. #define N 624
  54. #define M 397
  55. #define MATRIX_A 0x9908b0dfU /* constant vector a */
  56. #define UMASK 0x80000000U /* most significant w-r bits */
  57. #define LMASK 0x7fffffffU /* least significant r bits */
  58. #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
  59. #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
  60. enum {MT_MAX_STATE = N};
  61. struct MT {
  62. /* assume int is enough to store 32bits */
  63. unsigned int state[N]; /* the array for the state vector */
  64. unsigned int *next;
  65. int left;
  66. };
  67. #define genrand_initialized(mt) ((mt)->next != 0)
  68. #define uninit_genrand(mt) ((mt)->next = 0)
  69. /* initializes state[N] with a seed */
  70. static void
  71. init_genrand(struct MT *mt, unsigned int s)
  72. {
  73. int j;
  74. mt->state[0] = s & 0xffffffffU;
  75. for (j=1; j<N; j++) {
  76. mt->state[j] = (1812433253U * (mt->state[j-1] ^ (mt->state[j-1] >> 30)) + j);
  77. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  78. /* In the previous versions, MSBs of the seed affect */
  79. /* only MSBs of the array state[]. */
  80. /* 2002/01/09 modified by Makoto Matsumoto */
  81. mt->state[j] &= 0xffffffff; /* for >32 bit machines */
  82. }
  83. mt->left = 1;
  84. mt->next = mt->state + N;
  85. }
  86. /* initialize by an array with array-length */
  87. /* init_key is the array for initializing keys */
  88. /* key_length is its length */
  89. /* slight change for C++, 2004/2/26 */
  90. static void
  91. init_by_array(struct MT *mt, unsigned int init_key[], int key_length)
  92. {
  93. int i, j, k;
  94. init_genrand(mt, 19650218U);
  95. i=1; j=0;
  96. k = (N>key_length ? N : key_length);
  97. for (; k; k--) {
  98. mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1664525U))
  99. + init_key[j] + j; /* non linear */
  100. mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
  101. i++; j++;
  102. if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
  103. if (j>=key_length) j=0;
  104. }
  105. for (k=N-1; k; k--) {
  106. mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1566083941U))
  107. - i; /* non linear */
  108. mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
  109. i++;
  110. if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
  111. }
  112. mt->state[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
  113. }
  114. static void
  115. next_state(struct MT *mt)
  116. {
  117. unsigned int *p = mt->state;
  118. int j;
  119. /* if init_genrand() has not been called, */
  120. /* a default initial seed is used */
  121. if (!genrand_initialized(mt)) init_genrand(mt, 5489U);
  122. mt->left = N;
  123. mt->next = mt->state;
  124. for (j=N-M+1; --j; p++)
  125. *p = p[M] ^ TWIST(p[0], p[1]);
  126. for (j=M; --j; p++)
  127. *p = p[M-N] ^ TWIST(p[0], p[1]);
  128. *p = p[M-N] ^ TWIST(p[0], mt->state[0]);
  129. }
  130. /* generates a random number on [0,0xffffffff]-interval */
  131. static unsigned int
  132. genrand_int32(struct MT *mt)
  133. {
  134. unsigned int y;
  135. if (--mt->left <= 0) next_state(mt);
  136. y = *mt->next++;
  137. /* Tempering */
  138. y ^= (y >> 11);
  139. y ^= (y << 7) & 0x9d2c5680;
  140. y ^= (y << 15) & 0xefc60000;
  141. y ^= (y >> 18);
  142. return y;
  143. }
  144. /* generates a random number on [0,1) with 53-bit resolution*/
  145. static double
  146. genrand_real(struct MT *mt)
  147. {
  148. unsigned int a = genrand_int32(mt)>>5, b = genrand_int32(mt)>>6;
  149. return(a*67108864.0+b)*(1.0/9007199254740992.0);
  150. }
  151. /* generates a random number on [0,1] with 53-bit resolution*/
  152. // TODO
  153. #define genrand_real2 genrand_real
  154. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  155. #undef N
  156. #undef M