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 agents.anac.y2019.harddealer.math3.random;
|
---|
18 |
|
---|
19 |
|
---|
20 | /** This class implements the WELL19937a 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 | * @since 2.2
|
---|
32 |
|
---|
33 | */
|
---|
34 | public class Well19937a extends AbstractWell {
|
---|
35 |
|
---|
36 | /** Serializable version identifier. */
|
---|
37 | private static final long serialVersionUID = -7462102162223815419L;
|
---|
38 |
|
---|
39 | /** Number of bits in the pool. */
|
---|
40 | private static final int K = 19937;
|
---|
41 |
|
---|
42 | /** First parameter of the algorithm. */
|
---|
43 | private static final int M1 = 70;
|
---|
44 |
|
---|
45 | /** Second parameter of the algorithm. */
|
---|
46 | private static final int M2 = 179;
|
---|
47 |
|
---|
48 | /** Third parameter of the algorithm. */
|
---|
49 | private static final int M3 = 449;
|
---|
50 |
|
---|
51 | /** Creates a new random number generator.
|
---|
52 | * <p>The instance is initialized using the current time as the
|
---|
53 | * seed.</p>
|
---|
54 | */
|
---|
55 | public Well19937a() {
|
---|
56 | super(K, M1, M2, M3);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Creates a new random number generator using a single int seed.
|
---|
60 | * @param seed the initial seed (32 bits integer)
|
---|
61 | */
|
---|
62 | public Well19937a(int seed) {
|
---|
63 | super(K, M1, M2, M3, seed);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Creates a new random number generator using an int array seed.
|
---|
67 | * @param seed the initial seed (32 bits integers array), if null
|
---|
68 | * the seed of the generator will be related to the current time
|
---|
69 | */
|
---|
70 | public Well19937a(int[] seed) {
|
---|
71 | super(K, M1, M2, M3, seed);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Creates a new random number generator using a single long seed.
|
---|
75 | * @param seed the initial seed (64 bits integer)
|
---|
76 | */
|
---|
77 | public Well19937a(long seed) {
|
---|
78 | super(K, M1, M2, M3, seed);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** {@inheritDoc} */
|
---|
82 | @Override
|
---|
83 | protected int next(final int bits) {
|
---|
84 |
|
---|
85 | final int indexRm1 = iRm1[index];
|
---|
86 | final int indexRm2 = iRm2[index];
|
---|
87 |
|
---|
88 | final int v0 = v[index];
|
---|
89 | final int vM1 = v[i1[index]];
|
---|
90 | final int vM2 = v[i2[index]];
|
---|
91 | final int vM3 = v[i3[index]];
|
---|
92 |
|
---|
93 | final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]);
|
---|
94 | final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27));
|
---|
95 | final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1));
|
---|
96 | final int z3 = z1 ^ z2;
|
---|
97 | final int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21));
|
---|
98 |
|
---|
99 | v[index] = z3;
|
---|
100 | v[indexRm1] = z4;
|
---|
101 | v[indexRm2] &= 0x80000000;
|
---|
102 | index = indexRm1;
|
---|
103 |
|
---|
104 | return z4 >>> (32 - bits);
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|