source: src/main/java/agents/anac/y2019/harddealer/math3/random/AbstractWell.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 7.5 KB
Line 
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 */
17package agents.anac.y2019.harddealer.math3.random;
18
19import java.io.Serializable;
20
21import agents.anac.y2019.harddealer.math3.util.FastMath;
22
23
24/** This abstract class implements the WELL class of pseudo-random number generator
25 * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
26
27 * <p>This generator is described in a paper by Fran&ccedil;ois Panneton,
28 * Pierre L'Ecuyer and Makoto Matsumoto <a
29 * href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved
30 * Long-Period Generators Based on Linear Recurrences Modulo 2</a> ACM
31 * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper
32 * are in <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.</p>
33
34 * @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
35 * @since 2.2
36
37 */
38public abstract class AbstractWell extends BitsStreamGenerator implements Serializable {
39
40 /** Serializable version identifier. */
41 private static final long serialVersionUID = -817701723016583596L;
42
43 /** Current index in the bytes pool. */
44 protected int index;
45
46 /** Bytes pool. */
47 protected final int[] v;
48
49 /** Index indirection table giving for each index its predecessor taking table size into account. */
50 protected final int[] iRm1;
51
52 /** Index indirection table giving for each index its second predecessor taking table size into account. */
53 protected final int[] iRm2;
54
55 /** Index indirection table giving for each index the value index + m1 taking table size into account. */
56 protected final int[] i1;
57
58 /** Index indirection table giving for each index the value index + m2 taking table size into account. */
59 protected final int[] i2;
60
61 /** Index indirection table giving for each index the value index + m3 taking table size into account. */
62 protected final int[] i3;
63
64 /** Creates a new random number generator.
65 * <p>The instance is initialized using the current time plus the
66 * system identity hash code of this instance as the seed.</p>
67 * @param k number of bits in the pool (not necessarily a multiple of 32)
68 * @param m1 first parameter of the algorithm
69 * @param m2 second parameter of the algorithm
70 * @param m3 third parameter of the algorithm
71 */
72 protected AbstractWell(final int k, final int m1, final int m2, final int m3) {
73 this(k, m1, m2, m3, null);
74 }
75
76 /** Creates a new random number generator using a single int seed.
77 * @param k number of bits in the pool (not necessarily a multiple of 32)
78 * @param m1 first parameter of the algorithm
79 * @param m2 second parameter of the algorithm
80 * @param m3 third parameter of the algorithm
81 * @param seed the initial seed (32 bits integer)
82 */
83 protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int seed) {
84 this(k, m1, m2, m3, new int[] { seed });
85 }
86
87 /** Creates a new random number generator using an int array seed.
88 * @param k number of bits in the pool (not necessarily a multiple of 32)
89 * @param m1 first parameter of the algorithm
90 * @param m2 second parameter of the algorithm
91 * @param m3 third parameter of the algorithm
92 * @param seed the initial seed (32 bits integers array), if null
93 * the seed of the generator will be related to the current time
94 */
95 protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int[] seed) {
96
97 // the bits pool contains k bits, k = r w - p where r is the number
98 // of w bits blocks, w is the block size (always 32 in the original paper)
99 // and p is the number of unused bits in the last block
100 final int w = 32;
101 final int r = (k + w - 1) / w;
102 this.v = new int[r];
103 this.index = 0;
104
105 // precompute indirection index tables. These tables are used for optimizing access
106 // they allow saving computations like "(j + r - 2) % r" with costly modulo operations
107 iRm1 = new int[r];
108 iRm2 = new int[r];
109 i1 = new int[r];
110 i2 = new int[r];
111 i3 = new int[r];
112 for (int j = 0; j < r; ++j) {
113 iRm1[j] = (j + r - 1) % r;
114 iRm2[j] = (j + r - 2) % r;
115 i1[j] = (j + m1) % r;
116 i2[j] = (j + m2) % r;
117 i3[j] = (j + m3) % r;
118 }
119
120 // initialize the pool content
121 setSeed(seed);
122
123 }
124
125 /** Creates a new random number generator using a single long seed.
126 * @param k number of bits in the pool (not necessarily a multiple of 32)
127 * @param m1 first parameter of the algorithm
128 * @param m2 second parameter of the algorithm
129 * @param m3 third parameter of the algorithm
130 * @param seed the initial seed (64 bits integer)
131 */
132 protected AbstractWell(final int k, final int m1, final int m2, final int m3, final long seed) {
133 this(k, m1, m2, m3, new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
134 }
135
136 /** Reinitialize the generator as if just built with the given int seed.
137 * <p>The state of the generator is exactly the same as a new
138 * generator built with the same seed.</p>
139 * @param seed the initial seed (32 bits integer)
140 */
141 @Override
142 public void setSeed(final int seed) {
143 setSeed(new int[] { seed });
144 }
145
146 /** Reinitialize the generator as if just built with the given int array seed.
147 * <p>The state of the generator is exactly the same as a new
148 * generator built with the same seed.</p>
149 * @param seed the initial seed (32 bits integers array). If null
150 * the seed of the generator will be the system time plus the system identity
151 * hash code of the instance.
152 */
153 @Override
154 public void setSeed(final int[] seed) {
155 if (seed == null) {
156 setSeed(System.currentTimeMillis() + System.identityHashCode(this));
157 return;
158 }
159
160 System.arraycopy(seed, 0, v, 0, FastMath.min(seed.length, v.length));
161
162 if (seed.length < v.length) {
163 for (int i = seed.length; i < v.length; ++i) {
164 final long l = v[i - seed.length];
165 v[i] = (int) ((1812433253l * (l ^ (l >> 30)) + i) & 0xffffffffL);
166 }
167 }
168
169 index = 0;
170 clear(); // Clear normal deviate cache
171 }
172
173 /** Reinitialize the generator as if just built with the given long seed.
174 * <p>The state of the generator is exactly the same as a new
175 * generator built with the same seed.</p>
176 * @param seed the initial seed (64 bits integer)
177 */
178 @Override
179 public void setSeed(final long seed) {
180 setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
181 }
182
183 /** {@inheritDoc} */
184 @Override
185 protected abstract int next(final int bits);
186
187}
Note: See TracBrowser for help on using the repository browser.