Well44497a.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.  *      http://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. package org.apache.commons.rng.core.source32;

  18. /**
  19.  * This class implements the WELL44497a pseudo-random number generator
  20.  * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
  21.  * <p>
  22.  * This generator is described in a paper by Fran&ccedil;ois Panneton,
  23.  * Pierre L'Ecuyer and Makoto Matsumoto
  24.  * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
  25.  * Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
  26.  * ACM Transactions on Mathematical Software, 32, 1 (2006).
  27.  * The errata for the paper are in
  28.  * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
  29.  * </p>
  30.  *
  31.  * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
  32.  * @since 1.0
  33.  */
  34. public class Well44497a extends AbstractWell {
  35.     /** Number of bits in the pool. */
  36.     private static final int K = 44497;
  37.     /** First parameter of the algorithm. */
  38.     private static final int M1 = 23;
  39.     /** Second parameter of the algorithm. */
  40.     private static final int M2 = 481;
  41.     /** Third parameter of the algorithm. */
  42.     private static final int M3 = 229;
  43.     /** The indirection index table. */
  44.     private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);

  45.     /**
  46.      * Creates a new random number generator.
  47.      *
  48.      * @param seed Initial seed.
  49.      */
  50.     public Well44497a(int[] seed) {
  51.         super(K, seed);
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public int next() {
  56.         final int indexRm1 = TABLE.getIndexPred(index);
  57.         final int indexRm2 = TABLE.getIndexPred2(index);

  58.         final int v0 = v[index];
  59.         final int vM1 = v[TABLE.getIndexM1(index)];
  60.         final int vM2 = v[TABLE.getIndexM2(index)];
  61.         final int vM3 = v[TABLE.getIndexM3(index)];

  62.         // the values below include the errata of the original article
  63.         final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
  64.         final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30));
  65.         final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
  66.         final int z3 = z1 ^ z2;
  67.         final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
  68.         final int z2Second = ((z2 & 0x00020000) == 0) ? z2Prime : (z2Prime ^ 0xb729fcec);
  69.         final int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;

  70.         v[index] = z3;
  71.         v[indexRm1] = z4;
  72.         v[indexRm2] &= 0xFFFF8000;
  73.         index = indexRm1;

  74.         return z4;
  75.     }
  76. }