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