View Javadoc
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  /**
20   * This class implements the WELL44497a pseudo-random number generator
21   * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
22   * <p>
23   * This generator is described in a paper by Fran&ccedil;ois Panneton,
24   * Pierre L'Ecuyer and Makoto Matsumoto
25   * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
26   * Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
27   * ACM Transactions on Mathematical Software, 32, 1 (2006).
28   * The errata for the paper are in
29   * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
30   * </p>
31   *
32   * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
33   * @since 1.0
34   */
35  public class Well44497a extends AbstractWell {
36      /** Number of bits in the pool. */
37      private static final int K = 44497;
38      /** First parameter of the algorithm. */
39      private static final int M1 = 23;
40      /** Second parameter of the algorithm. */
41      private static final int M2 = 481;
42      /** Third parameter of the algorithm. */
43      private static final int M3 = 229;
44      /** The indirection index table. */
45      private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
46  
47      /**
48       * Creates a new random number generator.
49       *
50       * @param seed Initial seed.
51       */
52      public Well44497a(int[] seed) {
53          super(K, seed);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public int next() {
59          final int indexRm1 = TABLE.getIndexPred(index);
60          final int indexRm2 = TABLE.getIndexPred2(index);
61  
62          final int v0 = v[index];
63          final int vM1 = v[TABLE.getIndexM1(index)];
64          final int vM2 = v[TABLE.getIndexM2(index)];
65          final int vM3 = v[TABLE.getIndexM3(index)];
66  
67          // the values below include the errata of the original article
68          final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
69          final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30));
70          final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
71          final int z3 = z1 ^ z2;
72          final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
73          final int z2Second = ((z2 & 0x00020000) == 0) ? z2Prime : (z2Prime ^ 0xb729fcec);
74          final int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;
75  
76          v[index] = z3;
77          v[indexRm1] = z4;
78          v[indexRm2] &= 0xFFFF8000;
79          index = indexRm1;
80  
81          return z4;
82      }
83  }