Random.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Alternatively, the contents of this file may be used under the terms of
  15.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  16.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  17.  * in which case the provisions of the GPL or the LGPL are applicable instead
  18.  * of those above. If you wish to allow use of your version of this file only
  19.  * under the terms of either the GPL or the LGPL, and not to allow others to
  20.  * use your version of this file under the terms of the EPL, indicate your
  21.  * decision by deleting the provisions above and replace them with the notice
  22.  * and other provisions required by the GPL or the LGPL. If you do not delete
  23.  * the provisions above, a recipient may use your version of this file under
  24.  * the terms of any one of the EPL, the GPL or the LGPL.
  25.  ***** END LICENSE BLOCK *****/
  26. package org.jruby.util;

  27. import java.math.BigInteger;
  28. import java.util.Arrays;

  29. public class Random {
  30.     public static int N = 624;
  31.     private static int M = 397;
  32.     private static int MATRIX_A = 0x9908b0df; /* constant vector a */
  33.     private static int UMASK = 0x80000000; /* most significant w-r bits */
  34.     private static int LMASK = 0x7fffffff; /* least significant r bits */

  35.     private static int MIXBITS(int u, int v) {
  36.         return (u & UMASK) | (v & LMASK);
  37.     }

  38.     private static int TWIST(int u, int v) {
  39.         return (MIXBITS(u, v) >>> 1) ^ (((v & 1) != 0) ? MATRIX_A : 0);
  40.     }

  41.     private final int[] state = new int[N];
  42.     private int left = 1;

  43.     public Random(int s) {
  44.         initGenrand(s);
  45.     }

  46.     public Random(int[] initKey) {
  47.         initByArray(initKey);
  48.     }

  49.     public Random(Random orig) {
  50.         System.arraycopy(orig.state, 0, this.state, 0, this.state.length);
  51.         this.left = orig.left;
  52.     }

  53.     public Random(int[] state, int left) {
  54.         if (state.length != this.state.length) {
  55.             throw new IllegalStateException("wrong state length: " + state.length);
  56.         }
  57.         System.arraycopy(state, 0, this.state, 0, this.state.length);
  58.         this.left = left;
  59.     }

  60.     @Override
  61.     public boolean equals(Object obj) {
  62.         if (this == obj) {
  63.             return true;
  64.         } else if (!(obj instanceof Random)) {
  65.             return false;
  66.         }
  67.         Random rhs = (Random) obj;
  68.         return (left == rhs.left) && Arrays.equals(state, rhs.state);
  69.     }

  70.     @Override
  71.     public int hashCode() {
  72.         // Using 17 as the initializer, 37 as the multiplier.
  73.         return (629 + left) * 37 + state.hashCode();
  74.     }
  75.    
  76.     private void initGenrand(int s) {
  77.         state[0] = s;
  78.         for (int j = 1; j < N; j++) {
  79.             state[j] = (1812433253 * (state[j - 1] ^ (state[j - 1] >>> 30)) + j);
  80.         }
  81.         left = 1;
  82.     }

  83.     private void initByArray(int[] initKey) {
  84.         initGenrand(19650218);
  85.         int len = initKey.length;
  86.         int i = 1;
  87.         int j = 0;
  88.         int k = N > len ? N : len;
  89.         for (; k > 0; k--) {
  90.             state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >>> 30)) * 1664525)) + initKey[j]
  91.                     + j;
  92.             i++;
  93.             j++;
  94.             if (i >= N) {
  95.                 state[0] = state[N - 1];
  96.                 i = 1;
  97.             }
  98.             if (j >= len) {
  99.                 j = 0;
  100.             }
  101.         }
  102.         for (k = N - 1; k > 0; k--) {
  103.             state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >>> 30)) * 1566083941)) - i;
  104.             i++;
  105.             if (i >= N) {
  106.                 state[0] = state[N - 1];
  107.                 i = 1;
  108.             }
  109.         }
  110.         state[0] = 0x80000000;
  111.     }

  112.     private void nextState() {
  113.         int p = 0;

  114.         left = N;

  115.         for (int j = N - M + 1; --j > 0; p++) {
  116.             state[p] = state[p + M] ^ TWIST(state[p + 0], state[p + 1]);
  117.         }

  118.         for (int j = M; --j > 0; p++) {
  119.             state[p] = state[p + M - N] ^ TWIST(state[p + 0], state[p + 1]);
  120.         }

  121.         state[p] = state[p + M - N] ^ TWIST(state[p + 0], state[0]);
  122.     }

  123.     public int genrandInt32() {
  124.         int y;

  125.         synchronized (this) {
  126.             if (--left <= 0)
  127.                 nextState();

  128.             y = state[N - left];
  129.         }

  130.         /* Tempering */
  131.         y ^= (y >>> 11);
  132.         y ^= (y << 7) & 0x9d2c5680L;
  133.         y ^= (y << 15) & 0xefc60000L;
  134.         y ^= (y >>> 18);

  135.         return y;
  136.     }

  137.     public double genrandReal() {
  138.         int a = genrandInt32() >>> 5;
  139.         int b = genrandInt32() >>> 6;
  140.         return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
  141.     }

  142.     public double genrandReal2() {
  143.         int a = genrandInt32();
  144.         int b = genrandInt32();
  145.         return intPairToRealInclusive(a, b);
  146.     }

  147.     private static final BigInteger INTPAIR_CONST = BigInteger.valueOf((1L << 53) + 1);
  148.     private static final double LDEXP_CONST = Math.pow(2.0, -53);

  149.     // c: ldexp((a<< 32)|b) * ((1<<53)+1) >> 64, -53)
  150.     // TODO: not enough prec...
  151.     private double intPairToRealInclusive(int a, int b) {
  152.         BigInteger c = BigInteger.valueOf(a & 0xffffffffL);
  153.         BigInteger d = BigInteger.valueOf(b & 0xffffffffL);
  154.         return c.shiftLeft(32).or(d).multiply(INTPAIR_CONST).shiftRight(64).doubleValue()
  155.                 * LDEXP_CONST;
  156.     }

  157.     public int[] getState() {
  158.         return state;
  159.     }

  160.     public int getLeft() {
  161.         return left;
  162.     }
  163. }