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
019
020/** This class implements the WELL44497b pseudo-random number generator
021 * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
022
023 * <p>This generator is described in a paper by Fran&ccedil;ois Panneton,
024 * Pierre L'Ecuyer and Makoto Matsumoto <a
025 * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved
026 * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM
027 * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper
028 * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.</p>
029
030 * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
031 * @since 2.2
032
033 */
034public class Well44497b extends AbstractWell {
035
036    /** Serializable version identifier. */
037    private static final long serialVersionUID = 4032007538246675492L;
038
039    /** Number of bits in the pool. */
040    private static final int K = 44497;
041
042    /** First parameter of the algorithm. */
043    private static final int M1 = 23;
044
045    /** Second parameter of the algorithm. */
046    private static final int M2 = 481;
047
048    /** Third parameter of the algorithm. */
049    private static final int M3 = 229;
050
051    /** Creates a new random number generator.
052     * <p>The instance is initialized using the current time as the
053     * seed.</p>
054     */
055    public Well44497b() {
056        super(K, M1, M2, M3);
057    }
058
059    /** Creates a new random number generator using a single int seed.
060     * @param seed the initial seed (32 bits integer)
061     */
062    public Well44497b(int seed) {
063        super(K, M1, M2, M3, seed);
064    }
065
066    /** Creates a new random number generator using an int array seed.
067     * @param seed the initial seed (32 bits integers array), if null
068     * the seed of the generator will be related to the current time
069     */
070    public Well44497b(int[] seed) {
071        super(K, M1, M2, M3, seed);
072    }
073
074    /** Creates a new random number generator using a single long seed.
075     * @param seed the initial seed (64 bits integer)
076     */
077    public Well44497b(long seed) {
078        super(K, M1, M2, M3, seed);
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    protected int next(final int bits) {
084
085        // compute raw value given by WELL44497a generator
086        // which is NOT maximally-equidistributed
087        final int indexRm1 = iRm1[index];
088        final int indexRm2 = iRm2[index];
089
090        final int v0       = v[index];
091        final int vM1      = v[i1[index]];
092        final int vM2      = v[i2[index]];
093        final int vM3      = v[i3[index]];
094
095        // the values below include the errata of the original article
096        final int z0       = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
097        final int z1       = (v0 ^ (v0 << 24))  ^ (vM1 ^ (vM1 >>> 30));
098        final int z2       = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
099        final int z3       = z1      ^ z2;
100        final int z2Prime  = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
101        final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime;
102        int z4             = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;
103
104        v[index]     = z3;
105        v[indexRm1]  = z4;
106        v[indexRm2] &= 0xFFFF8000;
107        index        = indexRm1;
108
109        // add Matsumoto-Kurita tempering
110        // to get a maximally-equidistributed generator
111        z4 ^= (z4 <<  7) & 0x93dd1400;
112        z4 ^= (z4 << 15) & 0xfa118000;
113
114        return z4 >>> (32 - bits);
115
116    }
117
118}