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