$darkmode
DENOPTIM
Randomizer.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.utils;
20
21import java.util.Collection;
22import java.util.List;
23
24import javax.vecmath.Point3d;
25
26import org.apache.commons.math3.random.MersenneTwister;
27
28import denoptim.exception.DENOPTIMException;
29import denoptim.io.DenoptimIO;
30
35public class Randomizer
36{
41 private long rndSeed = 0L;
42
46 private MersenneTwister mt = null;
47
51 private final boolean debug = false;
52
53//------------------------------------------------------------------------------
54
58 public Randomizer()
59 {
61 }
62
63//------------------------------------------------------------------------------
64
68 public Randomizer(long seed)
69 {
70 initialiseRNG(seed);
71 }
72
73//------------------------------------------------------------------------------
74
79 private void setSeed(long value)
80 {
81 rndSeed = value;
82 }
83
84//------------------------------------------------------------------------------
85
89 public long getSeed()
90 {
91 return rndSeed;
92 }
93
94//------------------------------------------------------------------------------
95
100 public void initialiseRNG()
101 {
103 mt = new MersenneTwister(rndSeed);
104 }
105
106//------------------------------------------------------------------------------
107
112 public void initialiseRNG(long seed)
113 {
114 setSeed(seed);
115 mt = new MersenneTwister(rndSeed);
116 }
117
118//------------------------------------------------------------------------------
119
124 private MersenneTwister getRNG()
125 {
126 if (mt == null)
127 {
129 }
130 return mt;
131 }
132
133//------------------------------------------------------------------------------
134
138 private void print(Object val, String type)
139 {
140 String sss = "asked for "+ type + " "+val.toString();
141 if (true)
142 {
143 Exception ex = new Exception();
144 for (int i=0; i<5;i++)
145 {
146 String cn = ex.getStackTrace()[i].getClassName();
147 if (!cn.contains("RandomUtils"))
148 {
149 sss = sss + ex.getStackTrace()[i].getClassName() + ":"
150 + ex.getStackTrace()[i].getLineNumber()+" ";
151 }
152 }
153 }
154
155 try
156 {
157 DenoptimIO.writeData("/tmp/rng_debug_log",sss,true);
158 } catch (DENOPTIMException e)
159 {
160 e.printStackTrace();
161 }
162 }
163
164//------------------------------------------------------------------------------
165
171 public double nextDouble()
172 {
173 double d = getRNG().nextDouble();
174 if (debug)
175 print(d,"double");
176 return d;
177 }
178
179//------------------------------------------------------------------------------
180
188 public double nextNormalDouble()
189 {
190 double d = getRNG().nextGaussian();
191 if (debug)
192 print(d,"double");
193 return d;
194 }
195
196//------------------------------------------------------------------------------
197
205 public int nextInt(int i)
206 {
207 int r = getRNG().nextInt(i);
208 if (debug)
209 print(r,"int");
210 return r;
211 }
212
213//------------------------------------------------------------------------------
214
220 public boolean nextBoolean()
221 {
222 boolean r = getRNG().nextBoolean();
223 if (debug)
224 print(r,"boolean");
225 return r;
226 }
227
228//------------------------------------------------------------------------------
229
237 public boolean nextBoolean(double prob)
238 {
239 return nextDouble() < prob;
240 }
241
242//------------------------------------------------------------------------------
251 public Point3d getNoisyPoint(double maxAbsValue)
252 {
253 double xFactor = nextDouble();
254 double yFactor = nextDouble();
255 double zFactor = nextDouble();
256 double xSign = nextBoolean() ? 1.0 : -1.0;
257 double ySign = nextBoolean() ? 1.0 : -1.0;
258 double zSign = nextBoolean() ? 1.0 : -1.0;
259
260 return new Point3d(maxAbsValue*xFactor*xSign,
261 maxAbsValue*yFactor*ySign,
262 maxAbsValue*zFactor*zSign);
263 }
264
265//------------------------------------------------------------------------------
266
275 public Point3d getNormallyNoisyPoint(double maxAbsValue)
276 {
277 double xFactor = 2*nextNormalDouble()-1;
278 double yFactor = 2*nextNormalDouble()-1;
279 double zFactor = 2*nextNormalDouble()-1;
280
281 return new Point3d(maxAbsValue*xFactor,
282 maxAbsValue*yFactor,
283 maxAbsValue*zFactor);
284 }
285
286//------------------------------------------------------------------------------
287
300 public <T> T randomlyChooseOne(Collection<T> c)
301 {
302 if (c.size() == 0)
303 return null;
304
305 int chosen = nextInt(c.size());
306 int i=0;
307 T chosenObj = null;
308 for (T o : c)
309 {
310 if (i == chosen)
311 {
312 chosenObj = o;
313 }
314 i++;
315 }
316 return chosenObj;
317 }
318
319//------------------------------------------------------------------------------
320
327 public <T> void shuffle(List<T> list)
328 {
329 for (int i = list.size() - 1; i > 0; i--)
330 {
331 int j = nextInt(i + 1);
332 T tmp = list.get(i);
333 list.set(i, list.get(j));
334 list.set(j, tmp);
335 }
336 }
337
338//------------------------------------------------------------------------------
339
340 private void initialiseSeed()
341 {
342 // WARNING: The SecureRandom implementation in Linux is usable only
343 // once, then it becomes terribly slow: 1-2 minutes to get a seed!!!
344
345 /*
346 SecureRandom sec = new SecureRandom();
347 byte[] sbuf = sec.generateSeed(8);
348 ByteBuffer bb = ByteBuffer.wrap(sbuf);
349 rndSeed = bb.getLong();
350 */
351 rndSeed = System.currentTimeMillis();
352 }
353
354//------------------------------------------------------------------------------
355
356}
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Tool to generate random numbers and random decisions.
Definition: Randomizer.java:36
MersenneTwister mt
The implementation of the pseudo-random number generation.
Definition: Randomizer.java:46
boolean nextBoolean(double prob)
Returns whether the next pseudo-random, uniformly distributed double is lower than the specified valu...
long rndSeed
Seed used to control the generation of random numbers and decisions.
Definition: Randomizer.java:41
boolean nextBoolean()
Returns the next pseudo-random, uniformly distributed boolean value from this random number generator...
public< T > T randomlyChooseOne(Collection< T > c)
Chooses one member among the given collection.
Randomizer(long seed)
Constructor that specifies the random seed.
Definition: Randomizer.java:68
MersenneTwister getRNG()
Returns the random number generator.
int nextInt(int i)
Returns a pseudo-random, uniformly distributed int value between 0 (inclusive) and the specified valu...
Point3d getNormallyNoisyPoint(double maxAbsValue)
Returns a point in three-dimensional space with a random set of coordinates, the absolute value of wh...
final boolean debug
local flag used only to enable highly detailed logging.
Definition: Randomizer.java:51
double nextNormalDouble()
Returns the next pseudo-random, normally distributed double value between 0.0 and 1....
void initialiseRNG()
Initializes this random number generator (RNG) using a random seed that is generated on-the-fly rando...
double nextDouble()
Returns the next pseudo-random, uniformly distributed double value between 0.0 and 1....
Point3d getNoisyPoint(double maxAbsValue)
Returns a point in three-dimensional space with a random set of coordinates, the absolute value of wh...
void print(Object val, String type)
Utility to debug: writes some log in file '/tmp/rng_debug_log'.
void setSeed(long value)
Sets the random seed.
Definition: Randomizer.java:79
Randomizer()
Constructor.
Definition: Randomizer.java:58
void initialiseRNG(long seed)
Initialized this random number generator using the given seed.
public< T > void shuffle(List< T > list)
Fisher-Yates shuffle of the given list.