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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.random;
23  
24  
25  /**
26   * This class implements the WELL512a pseudo-random number generator
27   * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
28   * <p>
29   * This generator is described in a paper by Fran&ccedil;ois Panneton,
30   * Pierre L'Ecuyer and Makoto Matsumoto <a
31   * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved
32   * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM
33   * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper
34   * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">
35   * wellrng-errata.txt</a>.
36   *
37   * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
38   */
39  public class Well512a extends AbstractWell {
40  
41      /** Serializable version identifier. */
42      private static final long serialVersionUID = 20150223L;
43  
44      /** Number of bits in the pool. */
45      private static final int K = 512;
46  
47      /** First parameter of the algorithm. */
48      private static final int M1 = 13;
49  
50      /** Second parameter of the algorithm. */
51      private static final int M2 = 9;
52  
53      /** Third parameter of the algorithm. */
54      private static final int M3 = 5;
55  
56      /** The indirection index table. */
57      private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
58  
59      /**
60       * Creates a new random number generator.
61       * <p>
62       * The instance is initialized using the current time as the seed.
63       */
64      public Well512a() {
65          super(K);
66      }
67  
68      /**
69       * Creates a new random number generator using a single int seed.
70       * @param seed the initial seed (32 bits integer)
71       */
72      public Well512a(int seed) {
73          super(K, seed);
74      }
75  
76      /**
77       * Creates a new random number generator using an int array seed.
78       * @param seed the initial seed (32 bits integers array), if null
79       * the seed of the generator will be related to the current time
80       */
81      public Well512a(int[] seed) {
82          super(K, seed);
83      }
84  
85      /**
86       * Creates a new random number generator using a single long seed.
87       * @param seed the initial seed (64 bits integer)
88       */
89      public Well512a(long seed) {
90          super(K, seed);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public int nextInt() {
96  
97          final int indexRm1 = TABLE.getIndexPred(index);
98  
99          final int vi = v[index];
100         final int vi1 = v[TABLE.getIndexM1(index)];
101         final int vi2 = v[TABLE.getIndexM2(index)];
102         final int z0 = v[indexRm1];
103 
104         // the values below include the errata of the original article
105         final int z1 = (vi ^ (vi << 16))   ^ (vi1 ^ (vi1 << 15));
106         final int z2 = vi2 ^ (vi2 >>> 11);
107         final int z3 = z1 ^ z2;
108         final int z4 = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28) ^ (z3 ^ ((z3 << 5) & 0xda442d24));
109 
110         v[index] = z3;
111         v[indexRm1]  = z4;
112         index    = indexRm1;
113 
114         return z4;
115     }
116 
117 }