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 java.util.Random;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Extension of <code>java.util.Random</code> wrapping a
|
---|
23 | * {@link RandomGenerator}.
|
---|
24 | *
|
---|
25 | * @since 1.1
|
---|
26 | */
|
---|
27 | public class RandomAdaptor extends Random implements RandomGenerator {
|
---|
28 |
|
---|
29 | /** Serializable version identifier. */
|
---|
30 | private static final long serialVersionUID = 2306581345647615033L;
|
---|
31 |
|
---|
32 | /** Wrapped randomGenerator instance */
|
---|
33 | private final RandomGenerator randomGenerator;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Prevent instantiation without a generator argument
|
---|
37 | */
|
---|
38 | @SuppressWarnings("unused")
|
---|
39 | private RandomAdaptor() { randomGenerator = null; }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
|
---|
43 | *
|
---|
44 | * @param randomGenerator the wrapped generator
|
---|
45 | */
|
---|
46 | public RandomAdaptor(RandomGenerator randomGenerator) {
|
---|
47 | this.randomGenerator = randomGenerator;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Factory method to create a <code>Random</code> using the supplied
|
---|
52 | * <code>RandomGenerator</code>.
|
---|
53 | *
|
---|
54 | * @param randomGenerator wrapped RandomGenerator instance
|
---|
55 | * @return a Random instance wrapping the RandomGenerator
|
---|
56 | */
|
---|
57 | public static Random createAdaptor(RandomGenerator randomGenerator) {
|
---|
58 | return new RandomAdaptor(randomGenerator);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Returns the next pseudorandom, uniformly distributed
|
---|
63 | * <code>boolean</code> value from this random number generator's
|
---|
64 | * sequence.
|
---|
65 | *
|
---|
66 | * @return the next pseudorandom, uniformly distributed
|
---|
67 | * <code>boolean</code> value from this random number generator's
|
---|
68 | * sequence
|
---|
69 | */
|
---|
70 | @Override
|
---|
71 | public boolean nextBoolean() {
|
---|
72 | return randomGenerator.nextBoolean();
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Generates random bytes and places them into a user-supplied
|
---|
77 | * byte array. The number of random bytes produced is equal to
|
---|
78 | * the length of the byte array.
|
---|
79 | *
|
---|
80 | * @param bytes the non-null byte array in which to put the
|
---|
81 | * random bytes
|
---|
82 | */
|
---|
83 | @Override
|
---|
84 | public void nextBytes(byte[] bytes) {
|
---|
85 | randomGenerator.nextBytes(bytes);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns the next pseudorandom, uniformly distributed
|
---|
90 | * <code>double</code> value between <code>0.0</code> and
|
---|
91 | * <code>1.0</code> from this random number generator's sequence.
|
---|
92 | *
|
---|
93 | * @return the next pseudorandom, uniformly distributed
|
---|
94 | * <code>double</code> value between <code>0.0</code> and
|
---|
95 | * <code>1.0</code> from this random number generator's sequence
|
---|
96 | */
|
---|
97 | @Override
|
---|
98 | public double nextDouble() {
|
---|
99 | return randomGenerator.nextDouble();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Returns the next pseudorandom, uniformly distributed <code>float</code>
|
---|
104 | * value between <code>0.0</code> and <code>1.0</code> from this random
|
---|
105 | * number generator's sequence.
|
---|
106 | *
|
---|
107 | * @return the next pseudorandom, uniformly distributed <code>float</code>
|
---|
108 | * value between <code>0.0</code> and <code>1.0</code> from this
|
---|
109 | * random number generator's sequence
|
---|
110 | */
|
---|
111 | @Override
|
---|
112 | public float nextFloat() {
|
---|
113 | return randomGenerator.nextFloat();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Returns the next pseudorandom, Gaussian ("normally") distributed
|
---|
118 | * <code>double</code> value with mean <code>0.0</code> and standard
|
---|
119 | * deviation <code>1.0</code> from this random number generator's sequence.
|
---|
120 | *
|
---|
121 | * @return the next pseudorandom, Gaussian ("normally") distributed
|
---|
122 | * <code>double</code> value with mean <code>0.0</code> and
|
---|
123 | * standard deviation <code>1.0</code> from this random number
|
---|
124 | * generator's sequence
|
---|
125 | */
|
---|
126 | @Override
|
---|
127 | public double nextGaussian() {
|
---|
128 | return randomGenerator.nextGaussian();
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Returns the next pseudorandom, uniformly distributed <code>int</code>
|
---|
133 | * value from this random number generator's sequence.
|
---|
134 | * All 2<font size="-1"><sup>32</sup></font> possible {@code int} values
|
---|
135 | * should be produced with (approximately) equal probability.
|
---|
136 | *
|
---|
137 | * @return the next pseudorandom, uniformly distributed <code>int</code>
|
---|
138 | * value from this random number generator's sequence
|
---|
139 | */
|
---|
140 | @Override
|
---|
141 | public int nextInt() {
|
---|
142 | return randomGenerator.nextInt();
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Returns a pseudorandom, uniformly distributed {@code int} value
|
---|
147 | * between 0 (inclusive) and the specified value (exclusive), drawn from
|
---|
148 | * this random number generator's sequence.
|
---|
149 | *
|
---|
150 | * @param n the bound on the random number to be returned. Must be
|
---|
151 | * positive.
|
---|
152 | * @return a pseudorandom, uniformly distributed {@code int}
|
---|
153 | * value between 0 (inclusive) and n (exclusive).
|
---|
154 | * @throws IllegalArgumentException if n is not positive.
|
---|
155 | */
|
---|
156 | @Override
|
---|
157 | public int nextInt(int n) {
|
---|
158 | return randomGenerator.nextInt(n);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Returns the next pseudorandom, uniformly distributed <code>long</code>
|
---|
163 | * value from this random number generator's sequence. All
|
---|
164 | * 2<font size="-1"><sup>64</sup></font> possible {@code long} values
|
---|
165 | * should be produced with (approximately) equal probability.
|
---|
166 | *
|
---|
167 | * @return the next pseudorandom, uniformly distributed <code>long</code>
|
---|
168 | *value from this random number generator's sequence
|
---|
169 | */
|
---|
170 | @Override
|
---|
171 | public long nextLong() {
|
---|
172 | return randomGenerator.nextLong();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** {@inheritDoc} */
|
---|
176 | public void setSeed(int seed) {
|
---|
177 | if (randomGenerator != null) { // required to avoid NPE in constructor
|
---|
178 | randomGenerator.setSeed(seed);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** {@inheritDoc} */
|
---|
183 | public void setSeed(int[] seed) {
|
---|
184 | if (randomGenerator != null) { // required to avoid NPE in constructor
|
---|
185 | randomGenerator.setSeed(seed);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** {@inheritDoc} */
|
---|
190 | @Override
|
---|
191 | public void setSeed(long seed) {
|
---|
192 | if (randomGenerator != null) { // required to avoid NPE in constructor
|
---|
193 | randomGenerator.setSeed(seed);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|