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  import java.util.Arrays;
20  import org.apache.commons.rng.core.util.NumberFactory;
21  
22  /**
23   * This abstract class implements the WELL class of pseudo-random number
24   * generator from François Panneton, Pierre L'Ecuyer and Makoto
25   * Matsumoto.
26   * <p>
27   * This generator is described in a paper by Fran&ccedil;ois Panneton,
28   * Pierre L'Ecuyer and Makoto Matsumoto
29   * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
30   * Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
31   * ACM Transactions on Mathematical Software, 32, 1 (2006).
32   * The errata for the paper are in
33   * <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
34   * </p>
35   *
36   * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
37   *
38   * @since 1.0
39   */
40  public abstract class AbstractWell extends IntProvider {
41      /** Block size. */
42      private static final int BLOCK_SIZE = 32;
43      /** Current index in the bytes pool. */
44      protected int index;
45      /** Bytes pool. */
46      protected final int[] v;
47  
48      /**
49       * Creates an instance with the given {@code seed}.
50       *
51       * @param k Number of bits in the pool (not necessarily a multiple of 32).
52       * @param seed Initial seed.
53       */
54      protected AbstractWell(final int k,
55                             final int[] seed) {
56          final int r = calculateBlockCount(k);
57          v = new int[r];
58          index = 0;
59  
60          // Initialize the pool content.
61          setSeedInternal(seed);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      protected byte[] getStateInternal() {
67          final int[] s = Arrays.copyOf(v, v.length + 1);
68          s[v.length] = index;
69  
70          return NumberFactory.makeByteArray(s);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected void setStateInternal(byte[] s) {
76          checkStateSize(s, (v.length + 1) * 4);
77  
78          final int[] tmp = NumberFactory.makeIntArray(s);
79          System.arraycopy(tmp, 0, v, 0, v.length);
80          index = tmp[v.length];
81      }
82  
83      /**
84       * Initializes the generator with the given {@code seed}.
85       *
86       * @param seed Seed. Cannot be null.
87       */
88      private void setSeedInternal(final int[] seed) {
89          System.arraycopy(seed, 0, v, 0, Math.min(seed.length, v.length));
90  
91          if (seed.length < v.length) {
92              for (int i = seed.length; i < v.length; ++i) {
93                  final long current = v[i - seed.length];
94                  v[i] = (int) ((1812433253L * (current ^ (current >> 30)) + i) & 0xffffffffL);
95              }
96          }
97  
98          index = 0;
99      }
100 
101     /**
102      * Calculate the number of 32-bits blocks.
103      *
104      * @param k Number of bits in the pool (not necessarily a multiple of 32).
105      * @return the number of 32-bits blocks.
106      */
107     private static int calculateBlockCount(final int k) {
108         // The bits pool contains k bits, k = r w - p where r is the number
109         // of w bits blocks, w is the block size (always 32 in the original paper)
110         // and p is the number of unused bits in the last block.
111         return (k + BLOCK_SIZE - 1) / BLOCK_SIZE;
112     }
113 
114     /**
115      * Inner class used to store the indirection index table which is fixed for a given
116      * type of WELL class of pseudo-random number generator.
117      */
118     protected static final class IndexTable {
119         /** Index indirection table giving for each index its predecessor taking table size into account. */
120         private final int[] iRm1;
121         /** Index indirection table giving for each index its second predecessor taking table size into account. */
122         private final int[] iRm2;
123         /** Index indirection table giving for each index the value index + m1 taking table size into account. */
124         private final int[] i1;
125         /** Index indirection table giving for each index the value index + m2 taking table size into account. */
126         private final int[] i2;
127         /** Index indirection table giving for each index the value index + m3 taking table size into account. */
128         private final int[] i3;
129 
130         /** Creates a new pre-calculated indirection index table.
131          * @param k number of bits in the pool (not necessarily a multiple of 32)
132          * @param m1 first parameter of the algorithm
133          * @param m2 second parameter of the algorithm
134          * @param m3 third parameter of the algorithm
135          */
136         public IndexTable(final int k, final int m1, final int m2, final int m3) {
137 
138             final int r = calculateBlockCount(k);
139 
140             // precompute indirection index tables. These tables are used for optimizing access
141             // they allow saving computations like "(j + r - 2) % r" with costly modulo operations
142             iRm1 = new int[r];
143             iRm2 = new int[r];
144             i1 = new int[r];
145             i2 = new int[r];
146             i3 = new int[r];
147             for (int j = 0; j < r; ++j) {
148                 iRm1[j] = (j + r - 1) % r;
149                 iRm2[j] = (j + r - 2) % r;
150                 i1[j] = (j + m1) % r;
151                 i2[j] = (j + m2) % r;
152                 i3[j] = (j + m3) % r;
153             }
154         }
155 
156         /**
157          * Returns the predecessor of the given index modulo the table size.
158          * @param index the index to look at
159          * @return (index - 1) % table size
160          */
161         public int getIndexPred(final int index) {
162             return iRm1[index];
163         }
164 
165         /**
166          * Returns the second predecessor of the given index modulo the table size.
167          * @param index the index to look at
168          * @return (index - 2) % table size
169          */
170         public int getIndexPred2(final int index) {
171             return iRm2[index];
172         }
173 
174         /**
175          * Returns index + M1 modulo the table size.
176          * @param index the index to look at
177          * @return (index + M1) % table size
178          */
179         public int getIndexM1(final int index) {
180             return i1[index];
181         }
182 
183         /**
184          * Returns index + M2 modulo the table size.
185          * @param index the index to look at
186          * @return (index + M2) % table size
187          */
188         public int getIndexM2(final int index) {
189             return i2[index];
190         }
191 
192         /**
193          * Returns index + M3 modulo the table size.
194          * @param index the index to look at
195          * @return (index + M3) % table size
196          */
197         public int getIndexM3(final int index) {
198             return i3[index];
199         }
200     }
201 }