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