$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;
22
23import javax.vecmath.Point3d;
24
25import org.apache.commons.math3.random.MersenneTwister;
26
27import denoptim.exception.DENOPTIMException;
28import denoptim.io.DenoptimIO;
29
34public class Randomizer
35{
40 private long rndSeed = 0L;
41
45 private MersenneTwister mt = null;
46
50 private final boolean debug = false;
51
52//------------------------------------------------------------------------------
53
57 public Randomizer()
58 {
60 }
61
62//------------------------------------------------------------------------------
63
67 public Randomizer(long seed)
68 {
69 initialiseRNG(seed);
70 }
71
72//------------------------------------------------------------------------------
73
78 private void setSeed(long value)
79 {
80 rndSeed = value;
81 }
82
83//------------------------------------------------------------------------------
84
88 public long getSeed()
89 {
90 return rndSeed;
91 }
92
93//------------------------------------------------------------------------------
94
99 public void initialiseRNG()
100 {
102 mt = new MersenneTwister(rndSeed);
103 }
104
105//------------------------------------------------------------------------------
106
111 public void initialiseRNG(long seed)
112 {
113 setSeed(seed);
114 mt = new MersenneTwister(rndSeed);
115 }
116
117//------------------------------------------------------------------------------
118
123 private MersenneTwister getRNG()
124 {
125 if (mt == null)
126 {
128 }
129 return mt;
130 }
131
132//------------------------------------------------------------------------------
133
137 private void print(Object val, String type)
138 {
139 String sss = "asked for "+ type + " "+val.toString();
140 if (true)
141 {
142 Exception ex = new Exception();
143 for (int i=0; i<5;i++)
144 {
145 String cn = ex.getStackTrace()[i].getClassName();
146 if (!cn.contains("RandomUtils"))
147 {
148 sss = sss + ex.getStackTrace()[i].getClassName() + ":"
149 + ex.getStackTrace()[i].getLineNumber()+" ";
150 }
151 }
152 }
153
154 try
155 {
156 DenoptimIO.writeData("/tmp/rng_debug_log",sss,true);
157 } catch (DENOPTIMException e)
158 {
159 e.printStackTrace();
160 }
161 }
162
163//------------------------------------------------------------------------------
164
170 public double nextDouble()
171 {
172 double d = getRNG().nextDouble();
173 if (debug)
174 print(d,"double");
175 return d;
176 }
177
178//------------------------------------------------------------------------------
179
187 public double nextNormalDouble()
188 {
189 double d = getRNG().nextGaussian();
190 if (debug)
191 print(d,"double");
192 return d;
193 }
194
195//------------------------------------------------------------------------------
196
204 public int nextInt(int i)
205 {
206 int r = getRNG().nextInt(i);
207 if (debug)
208 print(r,"int");
209 return r;
210 }
211
212//------------------------------------------------------------------------------
213
219 public boolean nextBoolean()
220 {
221 boolean r = getRNG().nextBoolean();
222 if (debug)
223 print(r,"boolean");
224 return r;
225 }
226
227//------------------------------------------------------------------------------
228
236 public boolean nextBoolean(double prob)
237 {
238 return nextDouble() < prob;
239 }
240
241//------------------------------------------------------------------------------
250 public Point3d getNoisyPoint(double maxAbsValue)
251 {
252 double xFactor = nextDouble();
253 double yFactor = nextDouble();
254 double zFactor = nextDouble();
255 double xSign = nextBoolean() ? 1.0 : -1.0;
256 double ySign = nextBoolean() ? 1.0 : -1.0;
257 double zSign = nextBoolean() ? 1.0 : -1.0;
258
259 return new Point3d(maxAbsValue*xFactor*xSign,
260 maxAbsValue*yFactor*ySign,
261 maxAbsValue*zFactor*zSign);
262 }
263
264//------------------------------------------------------------------------------
265
274 public Point3d getNormallyNoisyPoint(double maxAbsValue)
275 {
276 double xFactor = 2*nextNormalDouble()-1;
277 double yFactor = 2*nextNormalDouble()-1;
278 double zFactor = 2*nextNormalDouble()-1;
279
280 return new Point3d(maxAbsValue*xFactor,
281 maxAbsValue*yFactor,
282 maxAbsValue*zFactor);
283 }
284
285//------------------------------------------------------------------------------
286
299 public <T> T randomlyChooseOne(Collection<T> c)
300 {
301 if (c.size() == 0)
302 return null;
303
304 int chosen = nextInt(c.size());
305 int i=0;
306 T chosenObj = null;
307 for (T o : c)
308 {
309 if (i == chosen)
310 {
311 chosenObj = o;
312 }
313 i++;
314 }
315 return chosenObj;
316 }
317
318//------------------------------------------------------------------------------
319
320 private void initialiseSeed()
321 {
322 // WARNING: The SecureRandom implementation in Linux is usable only
323 // once, then it becomes terribly slow: 1-2 minutes to get a seed!!!
324
325 /*
326 SecureRandom sec = new SecureRandom();
327 byte[] sbuf = sec.generateSeed(8);
328 ByteBuffer bb = ByteBuffer.wrap(sbuf);
329 rndSeed = bb.getLong();
330 */
331 rndSeed = System.currentTimeMillis();
332 }
333
334//------------------------------------------------------------------------------
335
336}
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:35
MersenneTwister mt
The implementation of the pseudo-random number generation.
Definition: Randomizer.java:45
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:40
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:67
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:50
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...
Definition: Randomizer.java:99
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:78
Randomizer()
Constructor.
Definition: Randomizer.java:57
void initialiseRNG(long seed)
Initialized this random number generator using the given seed.