You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mt19937ar.c 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. A C-program for MT19937, with initialization improved 2002/1/26.
  3. Coded by Takuji Nishimura and Makoto Matsumoto.
  4. Before using, initialize the state by using init_genrand(seed)
  5. or init_by_array(init_key, key_length).
  6. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. The names of its contributors may not be used to endorse or promote
  17. products derived from this software without specific prior written
  18. permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. Any feedback is very welcome.
  31. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  32. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  33. */
  34. #include "mt19937ar.h"
  35. /* Period parameters */
  36. #define N 624
  37. #define M 397
  38. #define MATRIX_A 0x9908b0dfUL /* constant vector a */
  39. #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
  40. #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
  41. static unsigned long mt[N]; /* the array for the state vector */
  42. static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
  43. /* initializes mt[N] with a seed */
  44. void init_genrand(unsigned long s)
  45. {
  46. mt[0]= s & 0xffffffffUL;
  47. for (mti=1; mti<N; mti++) {
  48. mt[mti] =
  49. (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
  50. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  51. /* In the previous versions, MSBs of the seed affect */
  52. /* only MSBs of the array mt[]. */
  53. /* 2002/01/09 modified by Makoto Matsumoto */
  54. mt[mti] &= 0xffffffffUL;
  55. /* for >32 bit machines */
  56. }
  57. }
  58. /* initialize by an array with array-length */
  59. /* init_key is the array for initializing keys */
  60. /* key_length is its length */
  61. /* slight change for C++, 2004/2/26 */
  62. void init_by_array(unsigned long init_key[], int key_length)
  63. {
  64. int i, j, k;
  65. init_genrand(19650218UL);
  66. i=1; j=0;
  67. k = (N>key_length ? N : key_length);
  68. for (; k; k--) {
  69. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
  70. + init_key[j] + j; /* non linear */
  71. mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  72. i++; j++;
  73. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  74. if (j>=key_length) j=0;
  75. }
  76. for (k=N-1; k; k--) {
  77. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
  78. - i; /* non linear */
  79. mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  80. i++;
  81. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  82. }
  83. mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
  84. }
  85. /* generates a random number on [0,0xffffffff]-interval */
  86. unsigned long genrand_int32(void)
  87. {
  88. unsigned long y;
  89. static unsigned long mag01[2]={0x0UL, MATRIX_A};
  90. /* mag01[x] = x * MATRIX_A for x=0,1 */
  91. if (mti >= N) { /* generate N words at one time */
  92. int kk;
  93. if (mti == N+1) /* if init_genrand() has not been called, */
  94. init_genrand(5489UL); /* a default initial seed is used */
  95. for (kk=0;kk<N-M;kk++) {
  96. y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  97. mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
  98. }
  99. for (;kk<N-1;kk++) {
  100. y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  101. mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
  102. }
  103. y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
  104. mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  105. mti = 0;
  106. }
  107. y = mt[mti++];
  108. /* Tempering */
  109. y ^= (y >> 11);
  110. y ^= (y << 7) & 0x9d2c5680UL;
  111. y ^= (y << 15) & 0xefc60000UL;
  112. y ^= (y >> 18);
  113. return y;
  114. }
  115. /* generates a random number on [0,0x7fffffff]-interval */
  116. long genrand_int31(void)
  117. {
  118. return (long)(genrand_int32()>>1);
  119. }
  120. /* generates a random number on [0,1]-real-interval */
  121. double genrand_real1(void)
  122. {
  123. return genrand_int32()*(1.0/4294967295.0);
  124. /* divided by 2^32-1 */
  125. }
  126. /* generates a random number on [0,1)-real-interval */
  127. double genrand_real2(void)
  128. {
  129. return genrand_int32()*(1.0/4294967296.0);
  130. /* divided by 2^32 */
  131. }
  132. /* generates a random number on (0,1)-real-interval */
  133. double genrand_real3(void)
  134. {
  135. return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
  136. /* divided by 2^32 */
  137. }
  138. /* generates a random number on [0,1) with 53-bit resolution*/
  139. double genrand_res53(void)
  140. {
  141. unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
  142. return(a*67108864.0+b)*(1.0/9007199254740992.0);
  143. }
  144. /* These real versions are due to Isaku Wada, 2002/01/09 added */