$darkmode
DENOPTIM
RandomizerTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.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 static org.junit.jupiter.api.Assertions.assertNull;
22import static org.junit.jupiter.api.Assertions.assertTrue;
23
24import java.util.ArrayList;
25import java.util.List;
26
27import javax.vecmath.Point3d;
28
29import org.junit.jupiter.api.Test;
30
37public class RandomizerTest
38{
39
40//------------------------------------------------------------------------------
41
42 @Test
43 public void testRandomizerReproducibility() throws Exception
44 {
45 double thrsld = 0.0000001;
46 int tot = 10000000;
47 long seed = 1234567;
48 Randomizer rngA = new Randomizer(seed);
49
50 double[] resA = new double[tot];
51 for (int i=0; i<tot; i++)
52 {
53 resA[i] = rngA.nextDouble();
54 }
55
56 Randomizer rngB = new Randomizer(seed);
57 double[] resB = new double[tot];
58 for (int i=0; i<tot; i++)
59 {
60 resB[i] = rngB.nextDouble();
61 }
62
63 for (int i=0; i<tot; i++)
64 {
65 assertTrue(thrsld > Math.abs(resA[i] - resB[i]),
66 "Inconsistent sequence of random doubles");
67 }
68 }
69
70//------------------------------------------------------------------------------
71
72 @Test
73 public void tesGetNoisyPoint() throws Exception
74 {
75 Randomizer rng = new Randomizer(123);
76 double maxVal = 12.34;
77 for (int i=0; i<10; i++)
78 {
79 Point3d p = rng.getNoisyPoint(maxVal);
80 assertTrue(maxVal >= Math.abs(p.x));
81 assertTrue(maxVal >= Math.abs(p.y));
82 assertTrue(maxVal >= Math.abs(p.z));
83 }
84 }
85
86//------------------------------------------------------------------------------
87
88 @Test
89 public void testRandomlyChooseOne() throws Exception
90 {
91 Randomizer rng = new Randomizer(123);
92 List<Integer> list = new ArrayList<Integer>();
93
94 // On empty list
95 for (int i=0; i<3; i++)
96 {
97 assertNull(rng.randomlyChooseOne(list));
98 }
99
100 // On filled list
101 for (int i=0; i<10; i++)
102 {
103 list.add(i);
104 }
105 for (int i=0; i<20; i++)
106 {
107 Integer chosen = rng.randomlyChooseOne(list);
108 assertTrue(chosen >= 0);
109 assertTrue(chosen < 10);
110 }
111
112
113 }
114
115//------------------------------------------------------------------------------
116
117}
Tool to generate random numbers and random decisions.
Definition: Randomizer.java:35
public< T > T randomlyChooseOne(Collection< T > c)
Chooses one member among the given collection.
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...