Well44497b.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.random;

  22. /** This class implements the WELL44497b pseudo-random number generator
  23.  * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
  24.  * <p>
  25.  * This generator is described in a paper by Fran&ccedil;ois Panneton,
  26.  * Pierre L'Ecuyer and Makoto Matsumoto <a
  27.  * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved
  28.  * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM
  29.  * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper
  30.  * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">
  31.  * wellrng-errata.txt</a>.
  32.  *
  33.  * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
  34.  */
  35. public class Well44497b extends AbstractWell {

  36.     /** Serializable version identifier. */
  37.     private static final long serialVersionUID = 20150223L;

  38.     /** Number of bits in the pool. */
  39.     private static final int K = 44497;

  40.     /** First parameter of the algorithm. */
  41.     private static final int M1 = 23;

  42.     /** Second parameter of the algorithm. */
  43.     private static final int M2 = 481;

  44.     /** Third parameter of the algorithm. */
  45.     private static final int M3 = 229;

  46.     /** The indirection index table. */
  47.     private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);

  48.     /**
  49.      * Creates a new random number generator.
  50.      * <p>
  51.      * The instance is initialized using the current time as the seed.
  52.      */
  53.     public Well44497b() {
  54.         super(K);
  55.     }

  56.     /**
  57.      * Creates a new random number generator using a single int seed.
  58.      * @param seed the initial seed (32 bits integer)
  59.      */
  60.     public Well44497b(int seed) {
  61.         super(K, seed);
  62.     }

  63.     /**
  64.      * Creates a new random number generator using an int array seed.
  65.      * @param seed the initial seed (32 bits integers array), if null
  66.      * the seed of the generator will be related to the current time
  67.      */
  68.     public Well44497b(int[] seed) {
  69.         super(K, seed);
  70.     }

  71.     /**
  72.      * Creates a new random number generator using a single long seed.
  73.      * @param seed the initial seed (64 bits integer)
  74.      */
  75.     public Well44497b(long seed) {
  76.         super(K, seed);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public int nextInt() {

  81.         // compute raw value given by WELL44497a generator
  82.         // which is NOT maximally-equidistributed
  83.         final int indexRm1 = TABLE.getIndexPred(index);
  84.         final int indexRm2 = TABLE.getIndexPred2(index);

  85.         final int v0       = v[index];
  86.         final int vM1      = v[TABLE.getIndexM1(index)];
  87.         final int vM2      = v[TABLE.getIndexM2(index)];
  88.         final int vM3      = v[TABLE.getIndexM3(index)];

  89.         // the values below include the errata of the original article
  90.         final int z0       = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
  91.         final int z1       = (v0 ^ (v0 << 24))  ^ (vM1 ^ (vM1 >>> 30));
  92.         final int z2       = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
  93.         final int z3       = z1      ^ z2;
  94.         final int z2Prime  = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
  95.         final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime;
  96.         int z4             = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;

  97.         v[index]     = z3;
  98.         v[indexRm1]  = z4;
  99.         v[indexRm2] &= 0xFFFF8000;
  100.         index        = indexRm1;

  101.         // add Matsumoto-Kurita tempering
  102.         // to get a maximally-equidistributed generator
  103.         z4 ^= (z4 <<  7) & 0x93dd1400;
  104.         z4 ^= (z4 << 15) & 0xfa118000;

  105.         return z4;
  106.     }

  107. }