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 | import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
|
---|
20 | import agents.anac.y2019.harddealer.math3.util.FastMath;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Abstract class implementing the {@link RandomGenerator} interface.
|
---|
24 | * Default implementations for all methods other than {@link #nextDouble()} and
|
---|
25 | * {@link #setSeed(long)} are provided.
|
---|
26 | * <p>
|
---|
27 | * All data generation methods are based on {@code code nextDouble()}.
|
---|
28 | * Concrete implementations <strong>must</strong> override
|
---|
29 | * this method and <strong>should</strong> provide better / more
|
---|
30 | * performant implementations of the other methods if the underlying PRNG
|
---|
31 | * supplies them.</p>
|
---|
32 | *
|
---|
33 | * @since 1.1
|
---|
34 | */
|
---|
35 | public abstract class AbstractRandomGenerator implements RandomGenerator {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Cached random normal value. The default implementation for
|
---|
39 | * {@link #nextGaussian} generates pairs of values and this field caches the
|
---|
40 | * second value so that the full algorithm is not executed for every
|
---|
41 | * activation. The value {@code Double.NaN} signals that there is
|
---|
42 | * no cached value. Use {@link #clear} to clear the cached value.
|
---|
43 | */
|
---|
44 | private double cachedNormalDeviate = Double.NaN;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Construct a RandomGenerator.
|
---|
48 | */
|
---|
49 | public AbstractRandomGenerator() {
|
---|
50 | super();
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Clears the cache used by the default implementation of
|
---|
56 | * {@link #nextGaussian}. Implementations that do not override the
|
---|
57 | * default implementation of {@code nextGaussian} should call this
|
---|
58 | * method in the implementation of {@link #setSeed(long)}
|
---|
59 | */
|
---|
60 | public void clear() {
|
---|
61 | cachedNormalDeviate = Double.NaN;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** {@inheritDoc} */
|
---|
65 | public void setSeed(int seed) {
|
---|
66 | setSeed((long) seed);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** {@inheritDoc} */
|
---|
70 | public void setSeed(int[] seed) {
|
---|
71 | // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5)
|
---|
72 | final long prime = 4294967291l;
|
---|
73 |
|
---|
74 | long combined = 0l;
|
---|
75 | for (int s : seed) {
|
---|
76 | combined = combined * prime + s;
|
---|
77 | }
|
---|
78 | setSeed(combined);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Sets the seed of the underlying random number generator using a
|
---|
83 | * {@code long} seed. Sequences of values generated starting with the
|
---|
84 | * same seeds should be identical.
|
---|
85 | * <p>
|
---|
86 | * Implementations that do not override the default implementation of
|
---|
87 | * {@code nextGaussian} should include a call to {@link #clear} in the
|
---|
88 | * implementation of this method.</p>
|
---|
89 | *
|
---|
90 | * @param seed the seed value
|
---|
91 | */
|
---|
92 | public abstract void setSeed(long seed);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Generates random bytes and places them into a user-supplied
|
---|
96 | * byte array. The number of random bytes produced is equal to
|
---|
97 | * the length of the byte array.
|
---|
98 | * <p>
|
---|
99 | * The default implementation fills the array with bytes extracted from
|
---|
100 | * random integers generated using {@link #nextInt}.</p>
|
---|
101 | *
|
---|
102 | * @param bytes the non-null byte array in which to put the
|
---|
103 | * random bytes
|
---|
104 | */
|
---|
105 | public void nextBytes(byte[] bytes) {
|
---|
106 | int bytesOut = 0;
|
---|
107 | while (bytesOut < bytes.length) {
|
---|
108 | int randInt = nextInt();
|
---|
109 | for (int i = 0; i < 3; i++) {
|
---|
110 | if ( i > 0) {
|
---|
111 | randInt >>= 8;
|
---|
112 | }
|
---|
113 | bytes[bytesOut++] = (byte) randInt;
|
---|
114 | if (bytesOut == bytes.length) {
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Returns the next pseudorandom, uniformly distributed {@code int}
|
---|
123 | * value from this random number generator's sequence.
|
---|
124 | * All 2<font size="-1"><sup>32</sup></font> possible {@code int} values
|
---|
125 | * should be produced with (approximately) equal probability.
|
---|
126 | * <p>
|
---|
127 | * The default implementation provided here returns
|
---|
128 | * <pre>
|
---|
129 | * <code>(int) (nextDouble() * Integer.MAX_VALUE)</code>
|
---|
130 | * </pre></p>
|
---|
131 | *
|
---|
132 | * @return the next pseudorandom, uniformly distributed {@code int}
|
---|
133 | * value from this random number generator's sequence
|
---|
134 | */
|
---|
135 | public int nextInt() {
|
---|
136 | return (int) ((2d * nextDouble() - 1d) * Integer.MAX_VALUE);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Returns a pseudorandom, uniformly distributed {@code int} value
|
---|
141 | * between 0 (inclusive) and the specified value (exclusive), drawn from
|
---|
142 | * this random number generator's sequence.
|
---|
143 | * <p>
|
---|
144 | * The default implementation returns
|
---|
145 | * <pre>
|
---|
146 | * <code>(int) (nextDouble() * n</code>
|
---|
147 | * </pre></p>
|
---|
148 | *
|
---|
149 | * @param n the bound on the random number to be returned. Must be
|
---|
150 | * positive.
|
---|
151 | * @return a pseudorandom, uniformly distributed {@code int}
|
---|
152 | * value between 0 (inclusive) and n (exclusive).
|
---|
153 | * @throws NotStrictlyPositiveException if {@code n <= 0}.
|
---|
154 | */
|
---|
155 | public int nextInt(int n) {
|
---|
156 | if (n <= 0 ) {
|
---|
157 | throw new NotStrictlyPositiveException(n);
|
---|
158 | }
|
---|
159 | int result = (int) (nextDouble() * n);
|
---|
160 | return result < n ? result : n - 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the next pseudorandom, uniformly distributed {@code long}
|
---|
165 | * value from this random number generator's sequence. All
|
---|
166 | * 2<font size="-1"><sup>64</sup></font> possible {@code long} values
|
---|
167 | * should be produced with (approximately) equal probability.
|
---|
168 | * <p>
|
---|
169 | * The default implementation returns
|
---|
170 | * <pre>
|
---|
171 | * <code>(long) (nextDouble() * Long.MAX_VALUE)</code>
|
---|
172 | * </pre></p>
|
---|
173 | *
|
---|
174 | * @return the next pseudorandom, uniformly distributed {@code long}
|
---|
175 | *value from this random number generator's sequence
|
---|
176 | */
|
---|
177 | public long nextLong() {
|
---|
178 | return (long) ((2d * nextDouble() - 1d) * Long.MAX_VALUE);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Returns the next pseudorandom, uniformly distributed
|
---|
183 | * {@code boolean} value from this random number generator's
|
---|
184 | * sequence.
|
---|
185 | * <p>
|
---|
186 | * The default implementation returns
|
---|
187 | * <pre>
|
---|
188 | * <code>nextDouble() <= 0.5</code>
|
---|
189 | * </pre></p>
|
---|
190 | *
|
---|
191 | * @return the next pseudorandom, uniformly distributed
|
---|
192 | * {@code boolean} value from this random number generator's
|
---|
193 | * sequence
|
---|
194 | */
|
---|
195 | public boolean nextBoolean() {
|
---|
196 | return nextDouble() <= 0.5;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Returns the next pseudorandom, uniformly distributed {@code float}
|
---|
201 | * value between {@code 0.0} and {@code 1.0} from this random
|
---|
202 | * number generator's sequence.
|
---|
203 | * <p>
|
---|
204 | * The default implementation returns
|
---|
205 | * <pre>
|
---|
206 | * <code>(float) nextDouble() </code>
|
---|
207 | * </pre></p>
|
---|
208 | *
|
---|
209 | * @return the next pseudorandom, uniformly distributed {@code float}
|
---|
210 | * value between {@code 0.0} and {@code 1.0} from this
|
---|
211 | * random number generator's sequence
|
---|
212 | */
|
---|
213 | public float nextFloat() {
|
---|
214 | return (float) nextDouble();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns the next pseudorandom, uniformly distributed
|
---|
219 | * {@code double} value between {@code 0.0} and
|
---|
220 | * {@code 1.0} from this random number generator's sequence.
|
---|
221 | * <p>
|
---|
222 | * This method provides the underlying source of random data used by the
|
---|
223 | * other methods.</p>
|
---|
224 | *
|
---|
225 | * @return the next pseudorandom, uniformly distributed
|
---|
226 | * {@code double} value between {@code 0.0} and
|
---|
227 | * {@code 1.0} from this random number generator's sequence
|
---|
228 | */
|
---|
229 | public abstract double nextDouble();
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Returns the next pseudorandom, Gaussian ("normally") distributed
|
---|
233 | * {@code double} value with mean {@code 0.0} and standard
|
---|
234 | * deviation {@code 1.0} from this random number generator's sequence.
|
---|
235 | * <p>
|
---|
236 | * The default implementation uses the <em>Polar Method</em>
|
---|
237 | * due to G.E.P. Box, M.E. Muller and G. Marsaglia, as described in
|
---|
238 | * D. Knuth, <u>The Art of Computer Programming</u>, 3.4.1C.</p>
|
---|
239 | * <p>
|
---|
240 | * The algorithm generates a pair of independent random values. One of
|
---|
241 | * these is cached for reuse, so the full algorithm is not executed on each
|
---|
242 | * activation. Implementations that do not override this method should
|
---|
243 | * make sure to call {@link #clear} to clear the cached value in the
|
---|
244 | * implementation of {@link #setSeed(long)}.</p>
|
---|
245 | *
|
---|
246 | * @return the next pseudorandom, Gaussian ("normally") distributed
|
---|
247 | * {@code double} value with mean {@code 0.0} and
|
---|
248 | * standard deviation {@code 1.0} from this random number
|
---|
249 | * generator's sequence
|
---|
250 | */
|
---|
251 | public double nextGaussian() {
|
---|
252 | if (!Double.isNaN(cachedNormalDeviate)) {
|
---|
253 | double dev = cachedNormalDeviate;
|
---|
254 | cachedNormalDeviate = Double.NaN;
|
---|
255 | return dev;
|
---|
256 | }
|
---|
257 | double v1 = 0;
|
---|
258 | double v2 = 0;
|
---|
259 | double s = 1;
|
---|
260 | while (s >=1 ) {
|
---|
261 | v1 = 2 * nextDouble() - 1;
|
---|
262 | v2 = 2 * nextDouble() - 1;
|
---|
263 | s = v1 * v1 + v2 * v2;
|
---|
264 | }
|
---|
265 | if (s != 0) {
|
---|
266 | s = FastMath.sqrt(-2 * FastMath.log(s) / s);
|
---|
267 | }
|
---|
268 | cachedNormalDeviate = v2 * s;
|
---|
269 | return v1 * s;
|
---|
270 | }
|
---|
271 | }
|
---|