src/java.base/share/classes/java/util/random/MRG32k3a.java
branchJDK-8193209-branch
changeset 57437 f02ffcb61dce
parent 57436 b0c958c0e6c6
child 57547 56cbdc3ea079
equal deleted inserted replaced
57436:b0c958c0e6c6 57437:f02ffcb61dce
    20  *
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package java.util;
    25 
       
    26 package java.util.random;
    26 
    27 
    27 import java.math.BigInteger;
    28 import java.math.BigInteger;
    28 import java.util.concurrent.atomic.AtomicLong;
    29 import java.util.concurrent.atomic.AtomicLong;
    29 
    30 
    30 /**
    31 /**
    31  * A generator of uniform pseudorandom values applicable for use in
    32  * A generator of uniform pseudorandom values applicable for use in
    32  * (among other contexts) isolated parallel computations that may
    33  * (among other contexts) isolated parallel computations that may
    33  * generate subtasks.  Class {@code MRG32k3a} implements
    34  * generate subtasks.  Class {@link MRG32k3a} implements
    34  * interfaces {@link java.util.Rng} and {@link java.util.AbstractArbitrarilyJumpableRng},
    35  * interfaces {@link RandomNumberGenerator} and {@link AbstractArbitrarilyJumpableRNG},
    35  * and therefore supports methods for producing pseudorandomly chosen
    36  * and therefore supports methods for producing pseudorandomly chosen
    36  * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
    37  * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
    37  * as well as creating new {@code Xoroshiro128PlusMRG32k3a} objects
    38  * as well as creating new {@link Xoroshiro128PlusMRG32k3a} objects
    38  * by "jumping" or "leaping".
    39  * by "jumping" or "leaping".
    39  *
    40  * <p>
    40  * <p>Instances {@code Xoroshiro128Plus} are <em>not</em> thread-safe.
    41  * Instances {@link Xoroshiro128Plus} are <em>not</em> thread-safe.
    41  * They are designed to be used so that each thread as its own instance.
    42  * They are designed to be used so that each thread as its own instance.
    42  * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
    43  * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
    43  * can be used to construct new instances of {@code Xoroshiro128Plus} that traverse
    44  * can be used to construct new instances of {@link Xoroshiro128Plus} that traverse
    44  * other parts of the state cycle.
    45  * other parts of the state cycle.
    45  *
    46  * <p>
    46  * <p>Instances of {@code MRG32k3a} are not cryptographically
    47  * Instances of {@link MRG32k3a} are not cryptographically
    47  * secure.  Consider instead using {@link java.security.SecureRandom}
    48  * secure.  Consider instead using {@link java.security.SecureRandom}
    48  * in security-sensitive applications. Additionally,
    49  * in security-sensitive applications. Additionally,
    49  * default-constructed instances do not use a cryptographically random
    50  * default-constructed instances do not use a cryptographically random
    50  * seed unless the {@linkplain System#getProperty system property}
    51  * seed unless the {@linkplain System#getProperty system property}
    51  * {@code java.util.secureRandomSeed} is set to {@code true}.
    52  * {@code java.util.secureRandomSeed} is set to {@code true}.
    52  *
    53  *
    53  * @author  Guy Steele
    54  * @since 14
    54  * @since   1.9
       
    55  */
    55  */
    56 public final class MRG32k3a extends AbstractArbitrarilyJumpableRng {
    56 public final class MRG32k3a extends AbstractArbitrarilyJumpableRNG {
    57 
    57 
    58     /*
    58     /*
    59      * Implementation Overview.
    59      * Implementation Overview.
    60      *
    60      *
    61      * See http://simul.iro.umontreal.ca/rng/MRG32k3a.c .
    61      * See http://simul.iro.umontreal.ca/rng/MRG32k3a.c .
    63      * File organization: First the non-public methods that constitute
    63      * File organization: First the non-public methods that constitute
    64      * the main algorithm, then the main public methods, followed by
    64      * the main algorithm, then the main public methods, followed by
    65      * some custom spliterator classes needed for stream methods.
    65      * some custom spliterator classes needed for stream methods.
    66      */
    66      */
    67 
    67 
    68     private final static double norm1 = 2.328306549295728e-10;
    68     private final static double NORM1 = 2.328306549295728e-10;
    69     private final static double norm2 = 2.328318824698632e-10;
    69     private final static double NORM2 = 2.328318824698632e-10;
    70     private final static double m1 =   4294967087.0;
    70     private final static double M1 =   4294967087.0;
    71     private final static double m2 =   4294944443.0;
    71     private final static double M2 =   4294944443.0;
    72     private final static double a12 =     1403580.0;
    72     private final static double A12 =     1403580.0;
    73     private final static double a13n =     810728.0;
    73     private final static double A13N =     810728.0;
    74     private final static double a21 =      527612.0;
    74     private final static double A21 =      527612.0;
    75     private final static double a23n =    1370589.0;
    75     private final static double A23N =    1370589.0;
    76     private final static int m1_deficit = 209;
    76     private final static int M1_DEFICIT = 209;
    77     
       
    78     // IllegalArgumentException messages
       
    79     private static final String BadLogDistance  = "logDistance must be non-negative and not greater than 192";
       
    80 
    77 
    81     /**
    78     /**
    82      * The per-instance state.
    79      * The per-instance state.
    83      The seeds for s10, s11, s12 must be integers in [0, m1 - 1] and not all 0. 
    80      The seeds for s10, s11, s12 must be integers in [0, m1 - 1] and not all 0.
    84      The seeds for s20, s21, s22 must be integers in [0, m2 - 1] and not all 0. 
    81      The seeds for s20, s21, s22 must be integers in [0, m2 - 1] and not all 0.
    85      */
    82      */
    86     private double s10, s11, s12,
    83     private double s10, s11, s12,
    87 	           s20, s21, s22;
    84                    s20, s21, s22;
    88 
    85 
    89     /**
    86     /**
    90      * The seed generator for default constructors.
    87      * The seed generator for default constructors.
    91      */
    88      */
    92     private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
    89     private static final AtomicLong DEFAULT_GEN =
       
    90         new AtomicLong(RNGSupport.initialSeed());
    93 
    91 
    94     /*
    92     /*
    95       32-bits Random number generator U(0,1): MRG32k3a
    93       32-bits Random number generator U(0,1): MRG32k3a
    96       Author: Pierre L'Ecuyer,
    94       Author: Pierre L'Ecuyer,
    97       Source: Good Parameter Sets for Combined Multiple Recursive Random
    95       Source: Good Parameter Sets for Combined Multiple Recursive Random
    98            Number Generators,
    96            Number Generators,
    99            Shorter version in Operations Research,
    97            Shorter version in Operations Research,
   100            47, 1 (1999), 159--164.
    98            47, 1 (1999), 159--164.
   101 	   ---------------------------------------------------------
    99            ---------------------------------------------------------
   102     */
   100     */
   103 
   101 
   104     private void nextState() {
   102     private void nextState() {
   105 	/* Component 1 */
   103         /* Component 1 */
   106 	double p1 = a12 * s11 - a13n * s10;
   104         double p1 = A12 * s11 - A13N * s10;
   107 	double k1 = p1 / m1;   p1 -= k1 * m1;   if (p1 < 0.0) p1 += m1;
   105         double k1 = p1 / M1;   p1 -= k1 * M1;   if (p1 < 0.0) p1 += M1;
   108 	s10 = s11;   s11 = s12;   s12 = p1;
   106         s10 = s11;   s11 = s12;   s12 = p1;
   109 	/* Component 2 */
   107         /* Component 2 */
   110 	double p2 = a21 * s22 - a23n * s20;
   108         double p2 = A21 * s22 - A23N * s20;
   111 	double k2 = p2 / m2;   p2 -= k2 * m2;   if (p2 < 0.0) p2 += m2;
   109         double k2 = p2 / M2;   p2 -= k2 * M2;   if (p2 < 0.0) p2 += M2;
   112 	s20 = s21;   s21 = s22;   s22 = p2;
   110         s20 = s21;   s21 = s22;   s22 = p2;
   113     }
   111     }
   114 
   112 
   115     
   113 
   116     /**
   114     /**
   117      * The form of nextInt used by IntStream Spliterators.
   115      * The form of nextInt used by IntStream Spliterators.
   118      * Exactly the same as long version, except for types.
   116      * Exactly the same as long version, except for types.
   119      *
   117      *
   120      * @param origin the least value, unless greater than bound
   118      * @param origin the least value, unless greater than bound
   121      * @param bound the upper bound (exclusive), must not equal origin
   119      * @param bound the upper bound (exclusive), must not equal origin
       
   120      *
   122      * @return a pseudorandom value
   121      * @return a pseudorandom value
   123      */
   122      */
   124     private int internalNextInt(int origin, int bound) {
   123     private int internalNextInt(int origin, int bound) {
   125         if (origin < bound) {
   124         if (origin < bound) {
   126             final int n = bound - origin;
   125             final int n = bound - origin;
   127 	    final int m = n - 1;
   126             final int m = n - 1;
   128 	    if (n > 0) {
   127             if (n > 0) {
   129 		int r;
   128                 int r;
   130                 for (int u = (int)nextDouble() >>> 1;
   129                 for (int u = (int)nextDouble() >>> 1;
   131                      u + m + ((m1_deficit + 1) >>> 1) - (r = u % n) < 0;
   130                      u + m + ((M1_DEFICIT + 1) >>> 1) - (r = u % n) < 0;
   132                      u = (int)nextDouble() >>> 1)
   131                      u = (int)nextDouble() >>> 1)
   133                     ;
   132                     ;
   134                 return (r + origin);
   133                 return (r + origin);
   135             } else {
   134             } else {
   136 		return RngSupport.boundedNextInt(this, origin, bound);
   135                 return RNGSupport.boundedNextInt(this, origin, bound);
   137             }
   136             }
   138         } else {
   137         } else {
   139 	    return nextInt();
   138             return nextInt();
   140 	}
   139         }
   141     }
   140     }
   142 
   141 
   143     private int internalNextInt(int bound) {
   142     private int internalNextInt(int bound) {
   144         // Specialize internalNextInt for origin == 0, bound > 0
   143         // Specialize internalNextInt for origin == 0, bound > 0
   145 	final int n = bound;
   144         final int n = bound;
   146 	final int m = n - 1;
   145         final int m = n - 1;
   147 	int r;
   146         int r;
   148 	for (int u = (int)nextDouble() >>> 1;
   147         for (int u = (int)nextDouble() >>> 1;
   149 	     u + m + ((m1_deficit + 1) >>> 1) - (r = u % n) < 0;
   148              u + m + ((M1_DEFICIT + 1) >>> 1) - (r = u % n) < 0;
   150 	     u = (int)nextDouble() >>> 1)
   149              u = (int)nextDouble() >>> 1)
   151 	    ;
   150             ;
   152 	return r;
   151         return r;
   153     }
   152     }
   154 
   153 
   155     /**
   154     /**
   156      * All arguments must be known to be nonnegative integral values
   155      * All arguments must be known to be nonnegative integral values
   157      * less than the appropriate modulus.
   156      * less than the appropriate modulus.
   158      */
   157      */
   159     private MRG32k3a(double s10, double s11, double s12,
   158     private MRG32k3a(double s10, double s11, double s12,
   160 		     double s20, double s21, double s22) {
   159                      double s20, double s21, double s22) {
   161 	this.s10 = s10; this.s11 = s11; this.s12 = s12;
   160         this.s10 = s10; this.s11 = s11; this.s12 = s12;
   162 	this.s20 = s20; this.s21 = s21; this.s22 = s22;
   161         this.s20 = s20; this.s21 = s21; this.s22 = s22;
   163 	if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) {
   162         if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) {
   164 	    this.s10 = this.s11 = this.s12 = 12345.0;
   163             this.s10 = this.s11 = this.s12 = 12345.0;
   165 	}
   164         }
   166 	if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) {
   165         if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) {
   167 	    this.s20 = this.s21 = this.s21 = 12345.0;
   166             this.s20 = this.s21 = this.s21 = 12345.0;
   168 	}
   167         }
   169     }
   168     }
   170 
   169 
   171     /* ---------------- public methods ---------------- */
   170     /* ---------------- public methods ---------------- */
   172 
   171 
   173     /**
   172     /**
   184      * @param s20 the first seed value for the second subgenerator
   183      * @param s20 the first seed value for the second subgenerator
   185      * @param s21 the second seed value for the second subgenerator
   184      * @param s21 the second seed value for the second subgenerator
   186      * @param s22 the third seed value for the second subgenerator
   185      * @param s22 the third seed value for the second subgenerator
   187      */
   186      */
   188     public MRG32k3a(int s10, int s11, int s12,
   187     public MRG32k3a(int s10, int s11, int s12,
   189 		    int s20, int s21, int s22) {
   188                     int s20, int s21, int s22) {
   190 	this(((double)(((long)s10) & 0x00000000ffffffffL)) % m1,
   189         this(((double)(((long)s10) & 0x00000000ffffffffL)) % M1,
   191 	     ((double)(((long)s11) & 0x00000000ffffffffL)) % m1,
   190              ((double)(((long)s11) & 0x00000000ffffffffL)) % M1,
   192 	     ((double)(((long)s12) & 0x00000000ffffffffL)) % m1,
   191              ((double)(((long)s12) & 0x00000000ffffffffL)) % M1,
   193 	     ((double)(((long)s20) & 0x00000000ffffffffL)) % m2,
   192              ((double)(((long)s20) & 0x00000000ffffffffL)) % M2,
   194 	     ((double)(((long)s21) & 0x00000000ffffffffL)) % m2,
   193              ((double)(((long)s21) & 0x00000000ffffffffL)) % M2,
   195 	     ((double)(((long)s22) & 0x00000000ffffffffL)) % m2);
   194              ((double)(((long)s22) & 0x00000000ffffffffL)) % M2);
   196     }
   195     }
   197 
   196 
   198     /**
   197     /**
   199      * Creates a new MRG32k3a instance using the specified
   198      * Creates a new MRG32k3a instance using the specified
   200      * initial seed. MRG32k3a instances created with the same
   199      * initial seed. MRG32k3a instances created with the same
   204      *
   203      *
   205      * @param seed the initial seed
   204      * @param seed the initial seed
   206      */
   205      */
   207     public MRG32k3a(long seed) {
   206     public MRG32k3a(long seed) {
   208         this((double)((seed & 0x7FF) + 12345),
   207         this((double)((seed & 0x7FF) + 12345),
   209 	     (double)(((seed >>> 11) & 0x7FF) + 12345),
   208              (double)(((seed >>> 11) & 0x7FF) + 12345),
   210 	     (double)(((seed >>> 22) & 0x7FF) + 12345),
   209              (double)(((seed >>> 22) & 0x7FF) + 12345),
   211 	     (double)(((seed >>> 33) & 0x7FF) + 12345),
   210              (double)(((seed >>> 33) & 0x7FF) + 12345),
   212 	     (double)(((seed >>> 44) & 0x7FF) + 12345),
   211              (double)(((seed >>> 44) & 0x7FF) + 12345),
   213 	     (double)((seed >>> 55) + 12345));
   212              (double)((seed >>> 55) + 12345));
   214     }
   213     }
   215 
   214 
   216     /**
   215     /**
   217      * Creates a new MRG32k3a instance that is likely to
   216      * Creates a new MRG32k3a instance that is likely to
   218      * generate sequences of values that are statistically independent
   217      * generate sequences of values that are statistically independent
   219      * of those of any other instances in the current program; and
   218      * of those of any other instances in the current program; and
   220      * may, and typically does, vary across program invocations.
   219      * may, and typically does, vary across program invocations.
   221      */
   220      */
   222     public MRG32k3a() {
   221     public MRG32k3a() {
   223 	this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
   222         this(DEFAULT_GEN.getAndAdd(RNGSupport.GOLDEN_RATIO_64));
   224     }
   223     }
   225 
   224 
   226     /**
   225     /**
   227      * Creates a new instance of {@code Xoshiro256StarStar} using the specified array of
   226      * Creates a new instance of {@link Xoshiro256StarStar} using the specified array of
   228      * initial seed bytes. Instances of {@code Xoshiro256StarStar} created with the same
   227      * initial seed bytes. Instances of {@link Xoshiro256StarStar} created with the same
   229      * seed array in the same program execution generate identical sequences of values.
   228      * seed array in the same program execution generate identical sequences of values.
   230      *
   229      *
   231      * @param seed the initial seed
   230      * @param seed the initial seed
   232      */
   231      */
   233     public MRG32k3a(byte[] seed) {
   232     public MRG32k3a(byte[] seed) {
   234 	// Convert the seed to 6 int values.
   233         // Convert the seed to 6 int values.
   235 	int[] data = RngSupport.convertSeedBytesToInts(seed, 6, 0);
   234         int[] data = RNGSupport.convertSeedBytesToInts(seed, 6, 0);
   236 	int s10 = data[0], s11 = data[1], s12 = data[2];
   235         int s10 = data[0], s11 = data[1], s12 = data[2];
   237 	int s20 = data[3], s21 = data[4], s22 = data[5];
   236         int s20 = data[3], s21 = data[4], s22 = data[5];
   238 	this.s10 = ((double)(((long)s10) & 0x00000000ffffffffL)) % m1;
   237         this.s10 = ((double)(((long)s10) & 0x00000000ffffffffL)) % M1;
   239 	this.s11 = ((double)(((long)s11) & 0x00000000ffffffffL)) % m1;
   238         this.s11 = ((double)(((long)s11) & 0x00000000ffffffffL)) % M1;
   240 	this.s12 = ((double)(((long)s12) & 0x00000000ffffffffL)) % m1;
   239         this.s12 = ((double)(((long)s12) & 0x00000000ffffffffL)) % M1;
   241 	this.s20 = ((double)(((long)s20) & 0x00000000ffffffffL)) % m2;
   240         this.s20 = ((double)(((long)s20) & 0x00000000ffffffffL)) % M2;
   242 	this.s21 = ((double)(((long)s21) & 0x00000000ffffffffL)) % m2;
   241         this.s21 = ((double)(((long)s21) & 0x00000000ffffffffL)) % M2;
   243 	this.s22 = ((double)(((long)s22) & 0x00000000ffffffffL)) % m2;
   242         this.s22 = ((double)(((long)s22) & 0x00000000ffffffffL)) % M2;
   244 	if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) {
   243         if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) {
   245 	    this.s10 = this.s11 = this.s12 = 12345.0;
   244             this.s10 = this.s11 = this.s12 = 12345.0;
   246 	}
   245         }
   247 	if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) {
   246         if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) {
   248 	    this.s20 = this.s21 = this.s21 = 12345.0;
   247             this.s20 = this.s21 = this.s21 = 12345.0;
   249 	}
   248         }
   250     }
   249     }
   251 
   250 
   252     public MRG32k3a copy() { return new MRG32k3a(s10, s11, s12, s20, s21, s22); }
   251     public MRG32k3a copy() {
       
   252         return new MRG32k3a(s10, s11, s12, s20, s21, s22);
       
   253     }
   253 
   254 
   254     /**
   255     /**
   255      * Returns a pseudorandom {@code double} value between zero
   256      * Returns a pseudorandom {@code double} value between zero
   256      * (exclusive) and one (exclusive).
   257      * (exclusive) and one (exclusive).
   257      *
   258      *
   258      * @return a pseudorandom {@code double} value between zero
   259      * @return a pseudorandom {@code double} value between zero
   259      *         (exclusive) and one (exclusive)
   260      *         (exclusive) and one (exclusive)
   260      */
   261      */
   261     public double nextOpenDouble() {
   262     public double nextOpenDouble() {
   262 	nextState();
   263         nextState();
   263 	double p1 = s12, p2 = s22;
   264         double p1 = s12, p2 = s22;
   264 	if (p1 <= p2)
   265         if (p1 <= p2)
   265 	    return ((p1 - p2 + m1) * norm1);
   266             return ((p1 - p2 + M1) * NORM1);
   266 	else
   267         else
   267 	    return ((p1 - p2) * norm1);
   268             return ((p1 - p2) * NORM1);
   268     }
   269     }
   269 
   270 
   270     /**
   271     /**
   271      * Returns a pseudorandom {@code double} value between zero
   272      * Returns a pseudorandom {@code double} value between zero
   272      * (inclusive) and one (exclusive).
   273      * (inclusive) and one (exclusive).
   273      *
   274      *
   274      * @return a pseudorandom {@code double} value between zero
   275      * @return a pseudorandom {@code double} value between zero
   275      *         (inclusive) and one (exclusive)
   276      *         (inclusive) and one (exclusive)
   276      */
   277      */
   277     public double nextDouble() {
   278     public double nextDouble() {
   278 	nextState();
   279         nextState();
   279 	double p1 = s12, p2 = s22;
   280         double p1 = s12, p2 = s22;
   280 	final double p = p1 * norm1 - p2 * norm2;
   281         final double p = p1 * NORM1 - p2 * NORM2;
   281 	if (p < 0.0) return (p + 1.0);
   282         if (p < 0.0) return (p + 1.0);
   282 	else return p;
   283         else return p;
   283     }
   284     }
   284 
   285 
   285     
   286 
   286     /**
   287     /**
   287      * Returns a pseudorandom {@code float} value between zero
   288      * Returns a pseudorandom {@code float} value between zero
   288      * (inclusive) and one (exclusive).
   289      * (inclusive) and one (exclusive).
   289      *
   290      *
   290      * @return a pseudorandom {@code float} value between zero
   291      * @return a pseudorandom {@code float} value between zero
   298      * Returns a pseudorandom {@code int} value.
   299      * Returns a pseudorandom {@code int} value.
   299      *
   300      *
   300      * @return a pseudorandom {@code int} value
   301      * @return a pseudorandom {@code int} value
   301      */
   302      */
   302     public int nextInt() {
   303     public int nextInt() {
   303 	return (internalNextInt(0x10000) << 16) | internalNextInt(0x10000);
   304         return (internalNextInt(0x10000) << 16) | internalNextInt(0x10000);
   304     }
   305     }
   305 
   306 
   306     /**
   307     /**
   307      * Returns a pseudorandom {@code long} value.
   308      * Returns a pseudorandom {@code long} value.
   308      *
   309      *
   309      * @return a pseudorandom {@code long} value
   310      * @return a pseudorandom {@code long} value
   310      */
   311      */
   311 
   312 
   312     public long nextLong() {
   313     public long nextLong() {
   313  	return (((long)internalNextInt(0x200000) << 43) |
   314          return (((long)internalNextInt(0x200000) << 43) |
   314 		((long)internalNextInt(0x200000) << 22) |
   315                 ((long)internalNextInt(0x200000) << 22) |
   315 		((long)internalNextInt(0x400000)));
   316                 ((long)internalNextInt(0x400000)));
   316     }
   317     }
   317 
   318 
   318     // Period is (m1**3 - 1)(m2**3 - 1)/2, or approximately 2**191.
   319     // Period is (m1**3 - 1)(m2**3 - 1)/2, or approximately 2**191.
   319     static BigInteger calculateThePeriod() {
   320     static BigInteger calculateThePeriod() {
   320 	BigInteger bigm1 = BigInteger.valueOf((long)m1);
   321         BigInteger bigm1 = BigInteger.valueOf((long)M1);
   321 	BigInteger bigm2 = BigInteger.valueOf((long)m2);
   322         BigInteger bigm2 = BigInteger.valueOf((long)M2);
   322 	BigInteger t1 = bigm1.multiply(bigm1).multiply(bigm1).subtract(BigInteger.ONE);
   323         BigInteger t1 = bigm1.multiply(bigm1).multiply(bigm1).subtract(BigInteger.ONE);
   323 	BigInteger t2 = bigm2.multiply(bigm2).multiply(bigm2).subtract(BigInteger.ONE);
   324         BigInteger t2 = bigm2.multiply(bigm2).multiply(bigm2).subtract(BigInteger.ONE);
   324 	return t1.shiftRight(1).multiply(t2);
   325         return t1.shiftRight(1).multiply(t2);
   325     }
   326     }
   326     static final BigInteger thePeriod = calculateThePeriod();
   327 
   327     public BigInteger period() { return thePeriod; }
   328     static final BigInteger PERIOD = calculateThePeriod();
       
   329 
       
   330     public BigInteger period() {
       
   331         return PERIOD;
       
   332     }
   328 
   333 
   329     // Jump and leap distances recommended in Section 1.3 of this paper:
   334     // Jump and leap distances recommended in Section 1.3 of this paper:
   330     // Pierre L'Ecuyer, Richard Simard, E. Jack Chen, and W. David Kelton.
   335     // Pierre L'Ecuyer, Richard Simard, E. Jack Chen, and W. David Kelton.
   331     // An Object-Oriented Random-Number Package with Many Long Streams and Substreams.
   336     // An Object-Oriented Random-Number Package with Many Long Streams and Substreams.
   332     // Operations Research 50, 6 (Nov--Dec 2002), 1073--1075.
   337     // Operations Research 50, 6 (Nov--Dec 2002), 1073--1075.
   333 
   338 
   334     public double defaultJumpDistance() { return 0x1.0p76; }  // 2**76
   339     public double defaultJumpDistance() {
   335     public double defaultLeapDistance() { return 0x1.0p127; }  // 2**127
   340         return 0x1.0p76;   // 2**76
   336         
   341     }
       
   342 
       
   343     public double defaultLeapDistance() {
       
   344         return 0x1.0p127;   // 2**127
       
   345     }
       
   346 
   337     public void jump(double distance) {
   347     public void jump(double distance) {
   338         if (distance < 0.0 || Double.isInfinite(distance) || distance != Math.floor(distance))
   348         if (distance < 0.0 || Double.isInfinite(distance) || distance != Math.floor(distance))
   339             throw new IllegalArgumentException("jump distance must be a nonnegative finite integer");
   349             throw new IllegalArgumentException("jump distance must be a nonnegative finite integer");
   340     	// We will compute a jump transformation (s => M s) for each LCG.
   350             // We will compute a jump transformation (s => M s) for each LCG.
   341     	// We initialize each transformation to the identity transformation.
   351             // We initialize each transformation to the identity transformation.
   342     	// Each will be turned into the d'th power of the corresponding base transformation.
   352             // Each will be turned into the d'th power of the corresponding base transformation.
   343 	long m1_00 = 1, m1_01 = 0, m1_02 = 0,
   353         long m1_00 = 1, m1_01 = 0, m1_02 = 0,
   344 	     m1_10 = 0, m1_11 = 1, m1_12 = 0,
   354              m1_10 = 0, m1_11 = 1, m1_12 = 0,
   345 	     m1_20 = 0, m1_21 = 0, m1_22 = 1;
   355              m1_20 = 0, m1_21 = 0, m1_22 = 1;
   346 	long m2_00 = 1, m2_01 = 0, m2_02 = 0,
   356         long m2_00 = 1, m2_01 = 0, m2_02 = 0,
   347 	     m2_10 = 0, m2_11 = 1, m2_12 = 0,
   357              m2_10 = 0, m2_11 = 1, m2_12 = 0,
   348 	     m2_20 = 0, m2_21 = 0, m2_22 = 1;
   358              m2_20 = 0, m2_21 = 0, m2_22 = 1;
   349 	// These are the base transformations, which will be repeatedly squared,
   359         // These are the base transformations, which will be repeatedly squared,
   350 	// and composed with the computed transformations for each 1-bit in distance.
   360         // and composed with the computed transformations for each 1-bit in distance.
   351 	long t1_00 = 0,           t1_01 = 1,         t1_02 = 0,
   361         long t1_00 = 0,           t1_01 = 1,         t1_02 = 0,
   352 	     t1_10 = 0,           t1_11 = 0,         t1_12 = 1,
   362              t1_10 = 0,           t1_11 = 0,         t1_12 = 1,
   353 	     t1_20 = -(long)a13n, t1_21 = (long)a12, t1_22 = 0;
   363              t1_20 = -(long)A13N, t1_21 = (long)A12, t1_22 = 0;
   354 	long t2_00 = 0,           t2_01 = 1,         t2_02 = 0,
   364         long t2_00 = 0,           t2_01 = 1,         t2_02 = 0,
   355 	     t2_10 = 0,           t2_11 = 0,         t2_12 = 1,
   365              t2_10 = 0,           t2_11 = 0,         t2_12 = 1,
   356 	     t2_20 = -(long)a23n, t2_21 = (long)a21, t2_22 = 0;
   366              t2_20 = -(long)A23N, t2_21 = (long)A21, t2_22 = 0;
   357 	while (distance > 0.0) {
   367         while (distance > 0.0) {
   358 	    final double dhalf = 0.5 * distance;
   368             final double dhalf = 0.5 * distance;
   359 	    if (Math.floor(dhalf) != dhalf) {
   369             if (Math.floor(dhalf) != dhalf) {
   360 		// distance is odd: accumulate current squaring
   370                 // distance is odd: accumulate current squaring
   361 		final long n1_00 = m1_00 * t1_00 + m1_01 * t1_10 + m1_02 * t1_20;
   371                 final long n1_00 = m1_00 * t1_00 + m1_01 * t1_10 + m1_02 * t1_20;
   362 		final long n1_01 = m1_00 * t1_01 + m1_01 * t1_11 + m1_02 * t1_21;
   372                 final long n1_01 = m1_00 * t1_01 + m1_01 * t1_11 + m1_02 * t1_21;
   363 		final long n1_02 = m1_00 * t1_02 + m1_01 * t1_12 + m1_02 * t1_22;
   373                 final long n1_02 = m1_00 * t1_02 + m1_01 * t1_12 + m1_02 * t1_22;
   364 		final long n1_10 = m1_10 * t1_00 + m1_11 * t1_10 + m1_12 * t1_20;
   374                 final long n1_10 = m1_10 * t1_00 + m1_11 * t1_10 + m1_12 * t1_20;
   365 		final long n1_11 = m1_10 * t1_01 + m1_11 * t1_11 + m1_12 * t1_21;
   375                 final long n1_11 = m1_10 * t1_01 + m1_11 * t1_11 + m1_12 * t1_21;
   366 		final long n1_12 = m1_10 * t1_02 + m1_11 * t1_12 + m1_12 * t1_22;
   376                 final long n1_12 = m1_10 * t1_02 + m1_11 * t1_12 + m1_12 * t1_22;
   367 		final long n1_20 = m1_20 * t1_00 + m1_21 * t1_10 + m1_22 * t1_20;
   377                 final long n1_20 = m1_20 * t1_00 + m1_21 * t1_10 + m1_22 * t1_20;
   368 		final long n1_21 = m1_20 * t1_01 + m1_21 * t1_11 + m1_22 * t1_21;
   378                 final long n1_21 = m1_20 * t1_01 + m1_21 * t1_11 + m1_22 * t1_21;
   369 		final long n1_22 = m1_20 * t1_02 + m1_21 * t1_12 + m1_22 * t1_22;
   379                 final long n1_22 = m1_20 * t1_02 + m1_21 * t1_12 + m1_22 * t1_22;
   370 		m1_00 = Math.floorMod(n1_00, (long)m1);
   380                 m1_00 = Math.floorMod(n1_00, (long)M1);
   371 		m1_01 = Math.floorMod(n1_01, (long)m1);
   381                 m1_01 = Math.floorMod(n1_01, (long)M1);
   372 		m1_02 = Math.floorMod(n1_02, (long)m1);
   382                 m1_02 = Math.floorMod(n1_02, (long)M1);
   373 		m1_10 = Math.floorMod(n1_10, (long)m1);
   383                 m1_10 = Math.floorMod(n1_10, (long)M1);
   374 		m1_11 = Math.floorMod(n1_11, (long)m1);
   384                 m1_11 = Math.floorMod(n1_11, (long)M1);
   375 		m1_12 = Math.floorMod(n1_12, (long)m1);
   385                 m1_12 = Math.floorMod(n1_12, (long)M1);
   376 		m1_20 = Math.floorMod(n1_20, (long)m1);
   386                 m1_20 = Math.floorMod(n1_20, (long)M1);
   377 		m1_21 = Math.floorMod(n1_21, (long)m1);
   387                 m1_21 = Math.floorMod(n1_21, (long)M1);
   378 		m1_22 = Math.floorMod(n1_22, (long)m1);
   388                 m1_22 = Math.floorMod(n1_22, (long)M1);
   379 		final long n2_00 = m2_00 * t2_00 + m2_01 * t2_10 + m2_02 * t2_20;
   389                 final long n2_00 = m2_00 * t2_00 + m2_01 * t2_10 + m2_02 * t2_20;
   380 		final long n2_01 = m2_00 * t2_01 + m2_01 * t2_11 + m2_02 * t2_21;
   390                 final long n2_01 = m2_00 * t2_01 + m2_01 * t2_11 + m2_02 * t2_21;
   381 		final long n2_02 = m2_00 * t2_02 + m2_01 * t2_12 + m2_02 * t2_22;
   391                 final long n2_02 = m2_00 * t2_02 + m2_01 * t2_12 + m2_02 * t2_22;
   382 		final long n2_10 = m2_10 * t2_00 + m2_11 * t2_10 + m2_12 * t2_20;
   392                 final long n2_10 = m2_10 * t2_00 + m2_11 * t2_10 + m2_12 * t2_20;
   383 		final long n2_11 = m2_10 * t2_01 + m2_11 * t2_11 + m2_12 * t2_21;
   393                 final long n2_11 = m2_10 * t2_01 + m2_11 * t2_11 + m2_12 * t2_21;
   384 		final long n2_12 = m2_10 * t2_02 + m2_11 * t2_12 + m2_12 * t2_22;
   394                 final long n2_12 = m2_10 * t2_02 + m2_11 * t2_12 + m2_12 * t2_22;
   385 		final long n2_20 = m2_20 * t2_00 + m2_21 * t2_10 + m2_22 * t2_20;
   395                 final long n2_20 = m2_20 * t2_00 + m2_21 * t2_10 + m2_22 * t2_20;
   386 		final long n2_21 = m2_20 * t2_01 + m2_21 * t2_11 + m2_22 * t2_21;
   396                 final long n2_21 = m2_20 * t2_01 + m2_21 * t2_11 + m2_22 * t2_21;
   387 		final long n2_22 = m2_20 * t2_02 + m2_21 * t2_12 + m2_22 * t2_22;
   397                 final long n2_22 = m2_20 * t2_02 + m2_21 * t2_12 + m2_22 * t2_22;
   388 		m2_00 = Math.floorMod(n2_00, (long)m2);
   398                 m2_00 = Math.floorMod(n2_00, (long)M2);
   389 		m2_01 = Math.floorMod(n2_01, (long)m2);
   399                 m2_01 = Math.floorMod(n2_01, (long)M2);
   390 		m2_02 = Math.floorMod(n2_02, (long)m2);
   400                 m2_02 = Math.floorMod(n2_02, (long)M2);
   391 		m2_10 = Math.floorMod(n2_10, (long)m2);
   401                 m2_10 = Math.floorMod(n2_10, (long)M2);
   392 		m2_11 = Math.floorMod(n2_11, (long)m2);
   402                 m2_11 = Math.floorMod(n2_11, (long)M2);
   393 		m2_12 = Math.floorMod(n2_12, (long)m2);
   403                 m2_12 = Math.floorMod(n2_12, (long)M2);
   394 		m2_20 = Math.floorMod(n2_20, (long)m2);
   404                 m2_20 = Math.floorMod(n2_20, (long)M2);
   395 		m2_21 = Math.floorMod(n2_21, (long)m2);
   405                 m2_21 = Math.floorMod(n2_21, (long)M2);
   396 		m2_22 = Math.floorMod(n2_22, (long)m2);
   406                 m2_22 = Math.floorMod(n2_22, (long)M2);
   397 	    }
   407             }
   398 	    // Square the base transformations.
   408             // Square the base transformations.
   399 	    {
   409             {
   400 		final long z1_00 = m1_00 * m1_00 + m1_01 * m1_10 + m1_02 * m1_20;
   410                 final long z1_00 = m1_00 * m1_00 + m1_01 * m1_10 + m1_02 * m1_20;
   401 		final long z1_01 = m1_00 * m1_01 + m1_01 * m1_11 + m1_02 * m1_21;
   411                 final long z1_01 = m1_00 * m1_01 + m1_01 * m1_11 + m1_02 * m1_21;
   402 		final long z1_02 = m1_00 * m1_02 + m1_01 * m1_12 + m1_02 * m1_22;
   412                 final long z1_02 = m1_00 * m1_02 + m1_01 * m1_12 + m1_02 * m1_22;
   403 		final long z1_10 = m1_10 * m1_00 + m1_11 * m1_10 + m1_12 * m1_20;
   413                 final long z1_10 = m1_10 * m1_00 + m1_11 * m1_10 + m1_12 * m1_20;
   404 		final long z1_11 = m1_10 * m1_01 + m1_11 * m1_11 + m1_12 * m1_21;
   414                 final long z1_11 = m1_10 * m1_01 + m1_11 * m1_11 + m1_12 * m1_21;
   405 		final long z1_12 = m1_10 * m1_02 + m1_11 * m1_12 + m1_12 * m1_22;
   415                 final long z1_12 = m1_10 * m1_02 + m1_11 * m1_12 + m1_12 * m1_22;
   406 		final long z1_20 = m1_20 * m1_00 + m1_21 * m1_10 + m1_22 * m1_20;
   416                 final long z1_20 = m1_20 * m1_00 + m1_21 * m1_10 + m1_22 * m1_20;
   407 		final long z1_21 = m1_20 * m1_01 + m1_21 * m1_11 + m1_22 * m1_21;
   417                 final long z1_21 = m1_20 * m1_01 + m1_21 * m1_11 + m1_22 * m1_21;
   408 		final long z1_22 = m1_20 * m1_02 + m1_21 * m1_12 + m1_22 * m1_22;
   418                 final long z1_22 = m1_20 * m1_02 + m1_21 * m1_12 + m1_22 * m1_22;
   409 		m1_00 = Math.floorMod(z1_00, (long)m1);
   419                 m1_00 = Math.floorMod(z1_00, (long)M1);
   410 		m1_01 = Math.floorMod(z1_01, (long)m1);
   420                 m1_01 = Math.floorMod(z1_01, (long)M1);
   411 		m1_02 = Math.floorMod(z1_02, (long)m1);
   421                 m1_02 = Math.floorMod(z1_02, (long)M1);
   412 		m1_10 = Math.floorMod(z1_10, (long)m1);
   422                 m1_10 = Math.floorMod(z1_10, (long)M1);
   413 		m1_11 = Math.floorMod(z1_11, (long)m1);
   423                 m1_11 = Math.floorMod(z1_11, (long)M1);
   414 		m1_12 = Math.floorMod(z1_12, (long)m1);
   424                 m1_12 = Math.floorMod(z1_12, (long)M1);
   415 		m1_20 = Math.floorMod(z1_20, (long)m1);
   425                 m1_20 = Math.floorMod(z1_20, (long)M1);
   416 		m1_21 = Math.floorMod(z1_21, (long)m1);
   426                 m1_21 = Math.floorMod(z1_21, (long)M1);
   417 		m1_22 = Math.floorMod(z1_22, (long)m1);
   427                 m1_22 = Math.floorMod(z1_22, (long)M1);
   418 		final long z2_00 = m2_00 * m2_00 + m2_01 * m2_10 + m2_02 * m2_20;
   428                 final long z2_00 = m2_00 * m2_00 + m2_01 * m2_10 + m2_02 * m2_20;
   419 		final long z2_01 = m2_00 * m2_01 + m2_01 * m2_11 + m2_02 * m2_21;
   429                 final long z2_01 = m2_00 * m2_01 + m2_01 * m2_11 + m2_02 * m2_21;
   420 		final long z2_02 = m2_00 * m2_02 + m2_01 * m2_12 + m2_02 * m2_22;
   430                 final long z2_02 = m2_00 * m2_02 + m2_01 * m2_12 + m2_02 * m2_22;
   421 		final long z2_10 = m2_10 * m2_00 + m2_11 * m2_10 + m2_12 * m2_20;
   431                 final long z2_10 = m2_10 * m2_00 + m2_11 * m2_10 + m2_12 * m2_20;
   422 		final long z2_11 = m2_10 * m2_01 + m2_11 * m2_11 + m2_12 * m2_21;
   432                 final long z2_11 = m2_10 * m2_01 + m2_11 * m2_11 + m2_12 * m2_21;
   423 		final long z2_12 = m2_10 * m2_02 + m2_11 * m2_12 + m2_12 * m2_22;
   433                 final long z2_12 = m2_10 * m2_02 + m2_11 * m2_12 + m2_12 * m2_22;
   424 		final long z2_20 = m2_20 * m2_00 + m2_21 * m2_10 + m2_22 * m2_20;
   434                 final long z2_20 = m2_20 * m2_00 + m2_21 * m2_10 + m2_22 * m2_20;
   425 		final long z2_21 = m2_20 * m2_01 + m2_21 * m2_11 + m2_22 * m2_21;
   435                 final long z2_21 = m2_20 * m2_01 + m2_21 * m2_11 + m2_22 * m2_21;
   426 		final long z2_22 = m2_20 * m2_02 + m2_21 * m2_12 + m2_22 * m2_22;
   436                 final long z2_22 = m2_20 * m2_02 + m2_21 * m2_12 + m2_22 * m2_22;
   427 		m2_00 = Math.floorMod(z2_00, (long)m2);
   437                 m2_00 = Math.floorMod(z2_00, (long)M2);
   428 		m2_01 = Math.floorMod(z2_01, (long)m2);
   438                 m2_01 = Math.floorMod(z2_01, (long)M2);
   429 		m2_02 = Math.floorMod(z2_02, (long)m2);
   439                 m2_02 = Math.floorMod(z2_02, (long)M2);
   430 		m2_10 = Math.floorMod(z2_10, (long)m2);
   440                 m2_10 = Math.floorMod(z2_10, (long)M2);
   431 		m2_11 = Math.floorMod(z2_11, (long)m2);
   441                 m2_11 = Math.floorMod(z2_11, (long)M2);
   432 		m2_12 = Math.floorMod(z2_12, (long)m2);
   442                 m2_12 = Math.floorMod(z2_12, (long)M2);
   433 		m2_20 = Math.floorMod(z2_20, (long)m2);
   443                 m2_20 = Math.floorMod(z2_20, (long)M2);
   434 		m2_21 = Math.floorMod(z2_21, (long)m2);
   444                 m2_21 = Math.floorMod(z2_21, (long)M2);
   435 		m2_22 = Math.floorMod(z2_22, (long)m2);
   445                 m2_22 = Math.floorMod(z2_22, (long)M2);
   436 	    }
   446             }
   437 	    // Divide distance by 2.
   447             // Divide distance by 2.
   438 	    distance = dhalf;
   448             distance = dhalf;
   439 	}
   449         }
   440 	final long w10 = m1_00 * (long)s10 + m1_01 * (long)s11 + m1_02 * (long)s12;
   450         final long w10 = m1_00 * (long)s10 + m1_01 * (long)s11 + m1_02 * (long)s12;
   441 	final long w11 = m1_10 * (long)s10 + m1_11 * (long)s11 + m1_12 * (long)s12;
   451         final long w11 = m1_10 * (long)s10 + m1_11 * (long)s11 + m1_12 * (long)s12;
   442 	final long w12 = m1_20 * (long)s10 + m1_21 * (long)s11 + m1_22 * (long)s12;
   452         final long w12 = m1_20 * (long)s10 + m1_21 * (long)s11 + m1_22 * (long)s12;
   443 	s10 = Math.floorMod(w10, (long)m1);
   453         s10 = Math.floorMod(w10, (long)M1);
   444 	s11 = Math.floorMod(w11, (long)m1);
   454         s11 = Math.floorMod(w11, (long)M1);
   445 	s12 = Math.floorMod(w12, (long)m1);
   455         s12 = Math.floorMod(w12, (long)M1);
   446 	final long w20 = m2_00 * (long)s20 + m2_01 * (long)s21 + m2_02 * (long)s22;
   456         final long w20 = m2_00 * (long)s20 + m2_01 * (long)s21 + m2_02 * (long)s22;
   447 	final long w21 = m2_10 * (long)s20 + m2_11 * (long)s21 + m2_12 * (long)s22;
   457         final long w21 = m2_10 * (long)s20 + m2_11 * (long)s21 + m2_12 * (long)s22;
   448 	final long w22 = m2_20 * (long)s20 + m2_21 * (long)s21 + m2_22 * (long)s22;
   458         final long w22 = m2_20 * (long)s20 + m2_21 * (long)s21 + m2_22 * (long)s22;
   449 	s20 = Math.floorMod(w20, (long)m2);
   459         s20 = Math.floorMod(w20, (long)M2);
   450 	s21 = Math.floorMod(w21, (long)m2);
   460         s21 = Math.floorMod(w21, (long)M2);
   451 	s22 = Math.floorMod(w22, (long)m2);
   461         s22 = Math.floorMod(w22, (long)M2);
   452     }
   462     }
   453         
   463 
   454     /**
   464     /**
   455      * Alter the state of this pseudorandom number generator so as to
   465      * Alter the state of this pseudorandom number generator so as to
   456      * jump forward a distance equal to 2<sup>{@code logDistance}</sup>
   466      * jump forward a distance equal to 2<sup>{@code logDistance}</sup>
   457      * within its state cycle.
   467      * within its state cycle.
   458      *
   468      *
   459      * @param logDistance the base-2 logarithm of the distance to jump
   469      * @param logDistance the base-2 logarithm of the distance to jump
   460      *        forward within the state cycle.  Must be non-negative and
   470      *        forward within the state cycle.  Must be non-negative and
   461      *        not greater than 192.
   471      *        not greater than 192.
       
   472      *
   462      * @throws IllegalArgumentException if {@code logDistance} is
   473      * @throws IllegalArgumentException if {@code logDistance} is
   463      *         less than zero or 2<sup>{@code logDistance}</sup> is
   474      *         less than zero or 2<sup>{@code logDistance}</sup> is
   464      *         greater than the period of this generator
   475      *         greater than the period of this generator
   465      */
   476      */
   466     public void jumpPowerOfTwo(int logDistance) {
   477     public void jumpPowerOfTwo(int logDistance) {
   467         if (logDistance < 0 || logDistance > 192)
   478         if (logDistance < 0 || logDistance > 192)
   468             throw new IllegalArgumentException(BadLogDistance);
   479             throw new IllegalArgumentException("logDistance must be non-negative and not greater than 192");
   469 	jump(Math.scalb(1.0, logDistance));
   480         jump(Math.scalb(1.0, logDistance));
   470     }
   481     }
   471 
   482 
   472 }
   483 }