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 WELL1024a 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 Well1024a extends AbstractWell {
36      /** Number of bits in the pool. */
37      private static final int K = 1024;
38      /** First parameter of the algorithm. */
39      private static final int M1 = 3;
40      /** Second parameter of the algorithm. */
41      private static final int M2 = 24;
42      /** Third parameter of the algorithm. */
43      private static final int M3 = 10;
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 Well1024a(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 v0 = v[index];
62          final int vM1 = v[TABLE.getIndexM1(index)];
63          final int vM2 = v[TABLE.getIndexM2(index)];
64          final int vM3 = v[TABLE.getIndexM3(index)];
65  
66          final int z0 = v[indexRm1];
67          final int z1 = v0 ^ (vM1 ^ (vM1 >>> 8));
68          final int z2 = (vM2 ^ (vM2 << 19)) ^ (vM3 ^ (vM3 << 14));
69          final int z3 = z1 ^ z2;
70          final int z4 = (z0 ^ (z0 << 11)) ^ (z1 ^ (z1 << 7)) ^ (z2 ^ (z2 << 13));
71  
72          v[index] = z3;
73          v[indexRm1] = z4;
74          index = indexRm1;
75  
76          return z4;
77      }
78  }