001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.random;
018
019import java.io.Serializable;
020
021import org.apache.commons.math3.util.FastMath;
022
023
024/** This abstract class implements the WELL class of pseudo-random number generator
025 * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
026
027 * <p>This generator is described in a paper by Fran&ccedil;ois Panneton,
028 * Pierre L'Ecuyer and Makoto Matsumoto <a
029 * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved
030 * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM
031 * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper
032 * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.</p>
033
034 * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
035 * @version $Id: AbstractWell.java 1547633 2013-12-03 23:03:06Z tn $
036 * @since 2.2
037
038 */
039public abstract class AbstractWell extends BitsStreamGenerator implements Serializable {
040
041    /** Serializable version identifier. */
042    private static final long serialVersionUID = -817701723016583596L;
043
044    /** Current index in the bytes pool. */
045    protected int index;
046
047    /** Bytes pool. */
048    protected final int[] v;
049
050    /** Index indirection table giving for each index its predecessor taking table size into account. */
051    protected final int[] iRm1;
052
053    /** Index indirection table giving for each index its second predecessor taking table size into account. */
054    protected final int[] iRm2;
055
056    /** Index indirection table giving for each index the value index + m1 taking table size into account. */
057    protected final int[] i1;
058
059    /** Index indirection table giving for each index the value index + m2 taking table size into account. */
060    protected final int[] i2;
061
062    /** Index indirection table giving for each index the value index + m3 taking table size into account. */
063    protected final int[] i3;
064
065    /** Creates a new random number generator.
066     * <p>The instance is initialized using the current time plus the
067     * system identity hash code of this instance as the seed.</p>
068     * @param k number of bits in the pool (not necessarily a multiple of 32)
069     * @param m1 first parameter of the algorithm
070     * @param m2 second parameter of the algorithm
071     * @param m3 third parameter of the algorithm
072     */
073    protected AbstractWell(final int k, final int m1, final int m2, final int m3) {
074        this(k, m1, m2, m3, null);
075    }
076
077    /** Creates a new random number generator using a single int seed.
078     * @param k number of bits in the pool (not necessarily a multiple of 32)
079     * @param m1 first parameter of the algorithm
080     * @param m2 second parameter of the algorithm
081     * @param m3 third parameter of the algorithm
082     * @param seed the initial seed (32 bits integer)
083     */
084    protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int seed) {
085        this(k, m1, m2, m3, new int[] { seed });
086    }
087
088    /** Creates a new random number generator using an int array seed.
089     * @param k number of bits in the pool (not necessarily a multiple of 32)
090     * @param m1 first parameter of the algorithm
091     * @param m2 second parameter of the algorithm
092     * @param m3 third parameter of the algorithm
093     * @param seed the initial seed (32 bits integers array), if null
094     * the seed of the generator will be related to the current time
095     */
096    protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int[] seed) {
097
098        // the bits pool contains k bits, k = r w - p where r is the number
099        // of w bits blocks, w is the block size (always 32 in the original paper)
100        // and p is the number of unused bits in the last block
101        final int w = 32;
102        final int r = (k + w - 1) / w;
103        this.v      = new int[r];
104        this.index  = 0;
105
106        // precompute indirection index tables. These tables are used for optimizing access
107        // they allow saving computations like "(j + r - 2) % r" with costly modulo operations
108        iRm1 = new int[r];
109        iRm2 = new int[r];
110        i1   = new int[r];
111        i2   = new int[r];
112        i3   = new int[r];
113        for (int j = 0; j < r; ++j) {
114            iRm1[j] = (j + r - 1) % r;
115            iRm2[j] = (j + r - 2) % r;
116            i1[j]   = (j + m1)    % r;
117            i2[j]   = (j + m2)    % r;
118            i3[j]   = (j + m3)    % r;
119        }
120
121        // initialize the pool content
122        setSeed(seed);
123
124    }
125
126    /** Creates a new random number generator using a single long seed.
127     * @param k number of bits in the pool (not necessarily a multiple of 32)
128     * @param m1 first parameter of the algorithm
129     * @param m2 second parameter of the algorithm
130     * @param m3 third parameter of the algorithm
131     * @param seed the initial seed (64 bits integer)
132     */
133    protected AbstractWell(final int k, final int m1, final int m2, final int m3, final long seed) {
134        this(k, m1, m2, m3, new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
135    }
136
137    /** Reinitialize the generator as if just built with the given int seed.
138     * <p>The state of the generator is exactly the same as a new
139     * generator built with the same seed.</p>
140     * @param seed the initial seed (32 bits integer)
141     */
142    @Override
143    public void setSeed(final int seed) {
144        setSeed(new int[] { seed });
145    }
146
147    /** Reinitialize the generator as if just built with the given int array seed.
148     * <p>The state of the generator is exactly the same as a new
149     * generator built with the same seed.</p>
150     * @param seed the initial seed (32 bits integers array). If null
151     * the seed of the generator will be the system time plus the system identity
152     * hash code of the instance.
153     */
154    @Override
155    public void setSeed(final int[] seed) {
156        if (seed == null) {
157            setSeed(System.currentTimeMillis() + System.identityHashCode(this));
158            return;
159        }
160
161        System.arraycopy(seed, 0, v, 0, FastMath.min(seed.length, v.length));
162
163        if (seed.length < v.length) {
164            for (int i = seed.length; i < v.length; ++i) {
165                final long l = v[i - seed.length];
166                v[i] = (int) ((1812433253l * (l ^ (l >> 30)) + i) & 0xffffffffL);
167            }
168        }
169
170        index = 0;
171        clear();  // Clear normal deviate cache
172    }
173
174    /** Reinitialize the generator as if just built with the given long seed.
175     * <p>The state of the generator is exactly the same as a new
176     * generator built with the same seed.</p>
177     * @param seed the initial seed (64 bits integer)
178     */
179    @Override
180    public void setSeed(final long seed) {
181        setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
182    }
183
184    /** {@inheritDoc} */
185    @Override
186    protected abstract int next(final int bits);
187
188}