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 WELL19937c 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 * @version $Id: Well19937c.java 1538368 2013-11-03 13:57:37Z erans $
032 * @since 2.2
033
034 */
035public class Well19937c extends AbstractWell {
036
037    /** Serializable version identifier. */
038    private static final long serialVersionUID = -7203498180754925124L;
039
040    /** Number of bits in the pool. */
041    private static final int K = 19937;
042
043    /** First parameter of the algorithm. */
044    private static final int M1 = 70;
045
046    /** Second parameter of the algorithm. */
047    private static final int M2 = 179;
048
049    /** Third parameter of the algorithm. */
050    private static final int M3 = 449;
051
052    /** Creates a new random number generator.
053     * <p>The instance is initialized using the current time as the
054     * seed.</p>
055     */
056    public Well19937c() {
057        super(K, M1, M2, M3);
058    }
059
060    /** Creates a new random number generator using a single int seed.
061     * @param seed the initial seed (32 bits integer)
062     */
063    public Well19937c(int seed) {
064        super(K, M1, M2, M3, seed);
065    }
066
067    /** Creates a new random number generator using an int array seed.
068     * @param seed the initial seed (32 bits integers array), if null
069     * the seed of the generator will be related to the current time
070     */
071    public Well19937c(int[] seed) {
072        super(K, M1, M2, M3, seed);
073    }
074
075    /** Creates a new random number generator using a single long seed.
076     * @param seed the initial seed (64 bits integer)
077     */
078    public Well19937c(long seed) {
079        super(K, M1, M2, M3, seed);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    protected int next(final int bits) {
085
086        final int indexRm1 = iRm1[index];
087        final int indexRm2 = iRm2[index];
088
089        final int v0       = v[index];
090        final int vM1      = v[i1[index]];
091        final int vM2      = v[i2[index]];
092        final int vM3      = v[i3[index]];
093
094        final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]);
095        final int z1 = (v0 ^ (v0 << 25))  ^ (vM1 ^ (vM1 >>> 27));
096        final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1));
097        final int z3 = z1      ^ z2;
098        int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21));
099
100        v[index]     = z3;
101        v[indexRm1]  = z4;
102        v[indexRm2] &= 0x80000000;
103        index        = indexRm1;
104
105
106        // add Matsumoto-Kurita tempering
107        // to get a maximally-equidistributed generator
108        z4 ^= (z4 <<  7) & 0xe46e1700;
109        z4 ^= (z4 << 15) & 0x9b868000;
110
111        return z4 >>> (32 - bits);
112
113    }
114
115}