AbstractWell.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. import java.io.Serializable;

  23. import org.hipparchus.util.FastMath;


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

  39.     /** Serializable version identifier. */
  40.     private static final long serialVersionUID = 20150223L;

  41.     /** Current index in the bytes pool. */
  42.     protected int index;

  43.     /** Bytes pool. */
  44.     protected final int[] v;

  45.     /** Creates a new random number generator.
  46.      * <p>The instance is initialized using the current time plus the
  47.      * system identity hash code of this instance as the seed.</p>
  48.      * @param k number of bits in the pool (not necessarily a multiple of 32)
  49.      */
  50.     protected AbstractWell(final int k) {
  51.         this(k, null);
  52.     }

  53.     /** Creates a new random number generator using a single int seed.
  54.      * @param k number of bits in the pool (not necessarily a multiple of 32)
  55.      * @param seed the initial seed (32 bits integer)
  56.      */
  57.     protected AbstractWell(final int k, final int seed) {
  58.         this(k, new int[] { seed });
  59.     }

  60.     /**
  61.      * Creates a new random number generator using an int array seed.
  62.      * @param k number of bits in the pool (not necessarily a multiple of 32)
  63.      * @param seed the initial seed (32 bits integers array), if null
  64.      * the seed of the generator will be related to the current time
  65.      */
  66.     protected AbstractWell(final int k, final int[] seed) {

  67.         final int r = calculateBlockCount(k);
  68.         this.v      = new int[r];
  69.         this.index  = 0;

  70.         // initialize the pool content
  71.         setSeed(seed);
  72.     }

  73.     /**
  74.      * Creates a new random number generator using a single long seed.
  75.      * @param k number of bits in the pool (not necessarily a multiple of 32)
  76.      * @param seed the initial seed (64 bits integer)
  77.      */
  78.     protected AbstractWell(final int k, final long seed) {
  79.         this(k, new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
  80.     }

  81.     /**
  82.      * Reinitialize the generator as if just built with the given int array seed.
  83.      * <p>
  84.      * The state of the generator is exactly the same as a new
  85.      * generator built with the same seed.
  86.      *
  87.      * @param seed the initial seed (32 bits integers array). If null
  88.      * the seed of the generator will be the system time plus the system identity
  89.      * hash code of the instance.
  90.      */
  91.     @Override
  92.     public void setSeed(final int[] seed) {
  93.         if (seed == null) {
  94.             setSeed(System.currentTimeMillis() + System.identityHashCode(this));
  95.             return;
  96.         }

  97.         System.arraycopy(seed, 0, v, 0, FastMath.min(seed.length, v.length));

  98.         if (seed.length < v.length) {
  99.             for (int i = seed.length; i < v.length; ++i) {
  100.                 final long l = v[i - seed.length];
  101.                 v[i] = (int) ((1812433253l * (l ^ (l >> 30)) + i) & 0xffffffffL);
  102.             }
  103.         }

  104.         index = 0;
  105.         clearCache(); // Clear normal deviate cache
  106.     }

  107.     /**
  108.      * Calculate the number of 32-bits blocks.
  109.      * @param k number of bits in the pool (not necessarily a multiple of 32)
  110.      * @return the number of 32-bits blocks
  111.      */
  112.     private static int calculateBlockCount(final int k) {
  113.         // the bits pool contains k bits, k = r w - p where r is the number
  114.         // of w bits blocks, w is the block size (always 32 in the original paper)
  115.         // and p is the number of unused bits in the last block
  116.         final int w = 32;
  117.         return (k + w - 1) / w;
  118.     }

  119.     /**
  120.      * Inner class used to store the indirection index table which is fixed
  121.      * for a given type of WELL class of pseudo-random number generator.
  122.      */
  123.     protected static final class IndexTable {
  124.         /**
  125.          * Index indirection table giving for each index its predecessor
  126.          * taking table size into account.
  127.          */
  128.         private final int[] iRm1;

  129.         /**
  130.          * Index indirection table giving for each index its second predecessor
  131.          * taking table size into account.
  132.          */
  133.         private final int[] iRm2;

  134.         /**
  135.          * Index indirection table giving for each index the value index + m1
  136.          * taking table size into account.
  137.          */
  138.         private final int[] i1;

  139.         /**
  140.          * Index indirection table giving for each index the value index + m2
  141.          * taking table size into account.
  142.          */
  143.         private final int[] i2;

  144.         /**
  145.          * Index indirection table giving for each index the value index + m3
  146.          * taking table size into account.
  147.          */
  148.         private final int[] i3;

  149.         /**
  150.          * Creates a new pre-calculated indirection index table.
  151.          * @param k number of bits in the pool (not necessarily a multiple of 32)
  152.          * @param m1 first parameter of the algorithm
  153.          * @param m2 second parameter of the algorithm
  154.          * @param m3 third parameter of the algorithm
  155.          */
  156.         public IndexTable(final int k, final int m1, final int m2, final int m3) {

  157.             final int r = calculateBlockCount(k);

  158.             // precompute indirection index tables. These tables are used for optimizing access
  159.             // they allow saving computations like "(j + r - 2) % r" with costly modulo operations
  160.             iRm1 = new int[r];
  161.             iRm2 = new int[r];
  162.             i1   = new int[r];
  163.             i2   = new int[r];
  164.             i3   = new int[r];
  165.             for (int j = 0; j < r; ++j) {
  166.                 iRm1[j] = (j + r - 1) % r;
  167.                 iRm2[j] = (j + r - 2) % r;
  168.                 i1[j]   = (j + m1)    % r;
  169.                 i2[j]   = (j + m2)    % r;
  170.                 i3[j]   = (j + m3)    % r;
  171.             }
  172.         }

  173.         /**
  174.          * Returns the predecessor of the given index modulo the table size.
  175.          * @param index the index to look at
  176.          * @return (index - 1) % table size
  177.          */
  178.         public int getIndexPred(final int index) {
  179.             return iRm1[index];
  180.         }

  181.         /**
  182.          * Returns the second predecessor of the given index modulo the table size.
  183.          * @param index the index to look at
  184.          * @return (index - 2) % table size
  185.          */
  186.         public int getIndexPred2(final int index) {
  187.             return iRm2[index];
  188.         }

  189.         /**
  190.          * Returns index + M1 modulo the table size.
  191.          * @param index the index to look at
  192.          * @return (index + M1) % table size
  193.          */
  194.         public int getIndexM1(final int index) {
  195.             return i1[index];
  196.         }

  197.         /**
  198.          * Returns index + M2 modulo the table size.
  199.          * @param index the index to look at
  200.          * @return (index + M2) % table size
  201.          */
  202.         public int getIndexM2(final int index) {
  203.             return i2[index];
  204.         }

  205.         /**
  206.          * Returns index + M3 modulo the table size.
  207.          * @param index the index to look at
  208.          * @return (index + M3) % table size
  209.          */
  210.         public int getIndexM3(final int index) {
  211.             return i3[index];
  212.         }
  213.     }
  214. }