Raymond | dee0849 | 2015-04-02 10:43:13 -0700 | [diff] [blame] | 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.math.random; |
| 18 | |
| 19 | |
| 20 | /** This class implements the WELL512a pseudo-random number generator |
| 21 | * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. |
| 22 | |
| 23 | * <p>This generator is described in a paper by François Panneton, |
| 24 | * Pierre L'Ecuyer and Makoto Matsumoto <a |
| 25 | * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved |
| 26 | * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM |
| 27 | * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper |
| 28 | * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.</p> |
| 29 | |
| 30 | * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a> |
| 31 | * @version $Revision: 1003892 $ $Date: 2010-10-02 23:28:56 +0200 (sam. 02 oct. 2010) $ |
| 32 | * @since 2.2 |
| 33 | |
| 34 | */ |
| 35 | public class Well512a extends AbstractWell { |
| 36 | |
| 37 | /** Serializable version identifier. */ |
| 38 | private static final long serialVersionUID = -6104179812103820574L; |
| 39 | |
| 40 | /** Number of bits in the pool. */ |
| 41 | private static final int K = 512; |
| 42 | |
| 43 | /** First parameter of the algorithm. */ |
| 44 | private static final int M1 = 13; |
| 45 | |
| 46 | /** Second parameter of the algorithm. */ |
| 47 | private static final int M2 = 9; |
| 48 | |
| 49 | /** Third parameter of the algorithm. */ |
| 50 | private static final int M3 = 5; |
| 51 | |
| 52 | /** Creates a new random number generator. |
| 53 | * <p>The instance is initialized using the current time as the |
| 54 | * seed.</p> |
| 55 | */ |
| 56 | public Well512a() { |
| 57 | super(K, M1, M2, M3); |
| 58 | } |
| 59 | |
| 60 | /** Creates a new random number generator using a single int seed. |
| 61 | * @param seed the initial seed (32 bits integer) |
| 62 | */ |
| 63 | public Well512a(int seed) { |
| 64 | super(K, M1, M2, M3, seed); |
| 65 | } |
| 66 | |
| 67 | /** Creates a new random number generator using an int array seed. |
| 68 | * @param seed the initial seed (32 bits integers array), if null |
| 69 | * the seed of the generator will be related to the current time |
| 70 | */ |
| 71 | public Well512a(int[] seed) { |
| 72 | super(K, M1, M2, M3, seed); |
| 73 | } |
| 74 | |
| 75 | /** Creates a new random number generator using a single long seed. |
| 76 | * @param seed the initial seed (64 bits integer) |
| 77 | */ |
| 78 | public Well512a(long seed) { |
| 79 | super(K, M1, M2, M3, seed); |
| 80 | } |
| 81 | |
| 82 | /** {@inheritDoc} */ |
| 83 | @Override |
| 84 | protected int next(final int bits) { |
| 85 | |
| 86 | final int indexRm1 = iRm1[index]; |
| 87 | |
| 88 | final int vi = v[index]; |
| 89 | final int vi1 = v[i1[index]]; |
| 90 | final int vi2 = v[i2[index]]; |
| 91 | final int z0 = v[indexRm1]; |
| 92 | |
| 93 | // the values below include the errata of the original article |
| 94 | final int z1 = (vi ^ (vi << 16)) ^ (vi1 ^ (vi1 << 15)); |
| 95 | final int z2 = vi2 ^ (vi2 >>> 11); |
| 96 | final int z3 = z1 ^ z2; |
| 97 | final int z4 = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28) ^ (z3 ^ ((z3 << 5) & 0xda442d24)); |
| 98 | |
| 99 | v[index] = z3; |
| 100 | v[indexRm1] = z4; |
| 101 | index = indexRm1; |
| 102 | |
| 103 | return z4 >>> (32 - bits); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | } |