Java



メルセンヌツイスター

  1. //引用元
  2. //http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/JAVA/PATCH/MTRandom.java
  3.  
  4. import java.util.Random;
  5. public class MersenneTwister extends Random {
  6. private static final long serialVersionUID = -515082678588212038L;
  7.  
  8. // Constants used in the original C implementation
  9. private final static int UPPER_MASK = 0x80000000;
  10. private final static int LOWER_MASK = 0x7fffffff;
  11.  
  12. private final static int N = 624;
  13. private final static int M = 397;
  14. private final static int MAGIC[] = { 0x0, 0x9908b0df };
  15. private final static int MAGIC_FACTOR1 = 1812433253;
  16. private final static int MAGIC_FACTOR2 = 1664525;
  17. private final static int MAGIC_FACTOR3 = 1566083941;
  18. private final static int MAGIC_MASK1 = 0x9d2c5680;
  19. private final static int MAGIC_MASK2 = 0xefc60000;
  20. private final static int MAGIC_SEED = 19650218;
  21. private final static long DEFAULT_SEED = 5489L;
  22.  
  23. // Internal state
  24. private transient int[] mt;
  25. private transient int mti;
  26. private transient boolean compat = false;
  27.  
  28. // Temporary buffer used during setSeed(long)
  29. private transient int[] ibuf;
  30.  
  31. public MersenneTwister() {
  32. this.setSeed(System.currentTimeMillis());
  33. }
  34.  
  35. public MersenneTwister(boolean compatible) {
  36. super(0L);
  37. compat = compatible;
  38. setSeed(compat ? DEFAULT_SEED : System.currentTimeMillis());
  39. }
  40.  
  41. public MersenneTwister(long seed) {
  42. super(seed);
  43. this.setSeed(seed);
  44. }
  45.  
  46. public MersenneTwister(byte[] buf) {
  47. super(0L);
  48. setSeed(buf);
  49. }
  50.  
  51. public MersenneTwister(int[] buf) {
  52. super(0L);
  53. setSeed(buf);
  54. }
  55.  
  56. // Initializes mt[N] with a simple integer seed. This method is
  57. // required as part of the Mersenne Twister algorithm but need
  58. // not be made public.
  59. private final void setSeed(int seed) {
  60.  
  61. // Annoying runtime check for initialisation of internal data
  62. // caused by java.util.Random invoking setSeed() during init.
  63. // This is unavoidable because no fields in our instance will
  64. // have been initialised at this point, not even if the code
  65. // were placed at the declaration of the member variable.
  66. if (mt == null)
  67. mt = new int[N];
  68.  
  69. // ---- Begin Mersenne Twister Algorithm ----
  70. mt[0] = seed;
  71. for (mti = 1; mti < N; mti++) {
  72. mt[mti] = (MAGIC_FACTOR1 * (mt[mti - 1] ^ (mt[mti - 1] >>> 30)) + mti);
  73. }
  74. // ---- End Mersenne Twister Algorithm ----
  75. }
  76.  
  77. public final synchronized void setSeed(long seed) {
  78. if (compat) {
  79. setSeed((int) seed);
  80. } else {
  81.  
  82. // Annoying runtime check for initialisation of internal data
  83. // caused by java.util.Random invoking setSeed() during init.
  84. // This is unavoidable because no fields in our instance will
  85. // have been initialised at this point, not even if the code
  86. // were placed at the declaration of the member variable.
  87. if (ibuf == null)
  88. ibuf = new int[2];
  89.  
  90. ibuf[0] = (int) seed;
  91. ibuf[1] = (int) (seed >>> 32);
  92. setSeed(ibuf);
  93. }
  94. }
  95.  
  96. public final void setSeed(byte[] buf) {
  97. setSeed(pack(buf));
  98. }
  99.  
  100. public final synchronized void setSeed(int[] buf) {
  101. int length = buf.length;
  102. if (length == 0)
  103. throw new IllegalArgumentException("Seed buffer may not be empty");
  104. // ---- Begin Mersenne Twister Algorithm ----
  105. int i = 1, j = 0, k = (N > length ? N : length);
  106. setSeed(MAGIC_SEED);
  107. for (; k > 0; k--) {
  108. mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * MAGIC_FACTOR2))
  109. + buf[j] + j;
  110. i++;
  111. j++;
  112. if (i >= N) {
  113. mt[0] = mt[N - 1];
  114. i = 1;
  115. }
  116. if (j >= length)
  117. j = 0;
  118. }
  119. for (k = N - 1; k > 0; k--) {
  120. mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * MAGIC_FACTOR3))
  121. - i;
  122. i++;
  123. if (i >= N) {
  124. mt[0] = mt[N - 1];
  125. i = 1;
  126. }
  127. }
  128. mt[0] = UPPER_MASK; // MSB is 1; assuring non-zero initial array
  129. // ---- End Mersenne Twister Algorithm ----
  130. }
  131.  
  132. protected final synchronized int next(int bits) {
  133. // ---- Begin Mersenne Twister Algorithm ----
  134. int y, kk;
  135. if (mti >= N) { // generate N words at one time
  136.  
  137. // In the original C implementation, mti is checked here
  138. // to determine if initialisation has occurred; if not
  139. // it initialises this instance with DEFAULT_SEED (5489).
  140. // This is no longer necessary as initialisation of the
  141. // Java instance must result in initialisation occurring
  142. // Use the constructor MersenneTwister(true) to enable backwards
  143. // compatible behaviour.
  144.  
  145. for (kk = 0; kk < N - M; kk++) {
  146. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  147. mt[kk] = mt[kk + M] ^ (y >>> 1) ^ MAGIC[y & 0x1];
  148. }
  149. for (; kk < N - 1; kk++) {
  150. y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  151. mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ MAGIC[y & 0x1];
  152. }
  153. y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  154. mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAGIC[y & 0x1];
  155.  
  156. mti = 0;
  157. }
  158.  
  159. y = mt[mti++];
  160.  
  161. // Tempering
  162. y ^= (y >>> 11);
  163. y ^= (y << 7) & MAGIC_MASK1;
  164. y ^= (y << 15) & MAGIC_MASK2;
  165. y ^= (y >>> 18);
  166. // ---- End Mersenne Twister Algorithm ----
  167. return (y >>> (32 - bits));
  168. }
  169.  
  170. // This is a fairly obscure little code section to pack a
  171. // byte[] into an int[] in little endian ordering.
  172.  
  173. public static int[] pack(byte[] buf) {
  174. int k, blen = buf.length, ilen = ((buf.length + 3) >>> 2);
  175. int[] ibuf = new int[ilen];
  176. for (int n = 0; n < ilen; n++) {
  177. int m = (n + 1) << 2;
  178. if (m > blen)
  179. m = blen;
  180. for (k = buf[--m] & 0xff; (m & 0x3) != 0; k = (k << 8) | buf[--m]
  181. & 0xff)
  182. ;
  183. ibuf[n] = k;
  184. }
  185. return ibuf;
  186. }
  187.  
}

配列の簡単な出力方法


  1. import java.util.Arrays;
  2.  
  3. public class TestClass {
  4. public static void main(String[] args) {
  5. double d[] = new double[3];
  6. d[0] = 1.0;
  7. d[1] = 2.0;
  8. d[2] = 3.0;
  9. System.out.println( Arrays.toString( d ) );
  10.  
  11. String str[] = new String[3];
  12. str[0] = "abc";
  13. str[1] = "ABC";
  14. str[2] = "TEST";
  15. System.out.println( Arrays.toString( str ) );
  16. }
  17. }
  18.  
最終更新:2012年11月13日 09:03
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。