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 WELL512a 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 Well512a extends AbstractWell {
36      /** Number of bits in the pool. */
37      private static final int K = 512;
38      /** First parameter of the algorithm. */
39      private static final int M1 = 13;
40      /** Second parameter of the algorithm. */
41      private static final int M2 = 9;
42      /** Third parameter of the algorithm. */
43      private static final int M3 = 5;
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 Well512a(int[] seed) {
53          super(K, seed);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public int next() {
59          final int indexRm1 = TABLE.getIndexPred(index);
60  
61          final int vi = v[index];
62          final int vi1 = v[TABLE.getIndexM1(index)];
63          final int vi2 = v[TABLE.getIndexM2(index)];
64          final int z0 = v[indexRm1];
65  
66          // the values below include the errata of the original article
67          final int z1 = (vi ^ (vi << 16)) ^ (vi1 ^ (vi1 << 15));
68          final int z2 = vi2 ^ (vi2 >>> 11);
69          final int z3 = z1 ^ z2;
70          final int z4 = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28) ^ (z3 ^ ((z3 << 5) & 0xda442d24));
71  
72          v[index] = z3;
73          v[indexRm1] = z4;
74          index = indexRm1;
75  
76          return z4;
77      }
78  }