Skip to content

Commit 90cc103

Browse files
perf: make Random splittable
1 parent 392de7e commit 90cc103

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

core/src/main/java/ai/timefold/solver/core/impl/solver/random/DefaultRandomFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DefaultRandomFactory(RandomType randomType, Long randomSeed) {
3232
public RandomGenerator createRandom() {
3333
switch (randomType) {
3434
case JDK:
35-
return randomSeed == null ? new Random() : new Random(randomSeed);
35+
return randomSeed == null ? new DelegatingSplittableRandomGenerator(new Random().nextLong()) : new DelegatingSplittableRandomGenerator(randomSeed);
3636
case MERSENNE_TWISTER:
3737
return new RandomAdaptor(randomSeed == null ? new MersenneTwister() : new MersenneTwister(randomSeed));
3838
case WELL512A:
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package ai.timefold.solver.core.impl.solver.random;
2+
3+
import java.util.SplittableRandom;
4+
import java.util.random.RandomGenerator;
5+
6+
public final class DelegatingSplittableRandomGenerator implements RandomGenerator {
7+
RandomGenerator.SplittableGenerator delegate;
8+
9+
DelegatingSplittableRandomGenerator(long seed) {
10+
this.delegate = new SplittableRandom(seed);
11+
}
12+
13+
public RandomGenerator.SplittableGenerator split() {
14+
return delegate.split();
15+
}
16+
17+
public void setDelegate(RandomGenerator.SplittableGenerator delegate) {
18+
this.delegate = delegate;
19+
}
20+
21+
// *****************************************
22+
// RandomGenerator methods
23+
// *****************************************
24+
25+
@Override
26+
public long nextLong() {
27+
return delegate.nextLong();
28+
}
29+
30+
@Override
31+
public int nextInt() {
32+
return delegate.nextInt();
33+
}
34+
35+
@Override
36+
public int nextInt(int bound) {
37+
return delegate.nextInt(bound);
38+
}
39+
40+
@Override
41+
public int nextInt(int origin, int bound) {
42+
return delegate.nextInt(origin, bound);
43+
}
44+
45+
@Override
46+
public long nextLong(long bound) {
47+
return delegate.nextLong(bound);
48+
}
49+
50+
@Override
51+
public long nextLong(long origin, long bound) {
52+
return delegate.nextLong(origin, bound);
53+
}
54+
55+
@Override
56+
public double nextDouble() {
57+
return delegate.nextDouble();
58+
}
59+
60+
@Override
61+
public double nextDouble(double bound) {
62+
return delegate.nextDouble(bound);
63+
}
64+
65+
@Override
66+
public double nextDouble(double origin, double bound) {
67+
return delegate.nextDouble(origin, bound);
68+
}
69+
70+
@Override
71+
public float nextFloat() {
72+
return delegate.nextFloat();
73+
}
74+
75+
@Override
76+
public float nextFloat(float bound) {
77+
return delegate.nextFloat(bound);
78+
}
79+
80+
@Override
81+
public float nextFloat(float origin, float bound) {
82+
return delegate.nextFloat(origin, bound);
83+
}
84+
85+
@Override
86+
public double nextGaussian() {
87+
return delegate.nextGaussian();
88+
}
89+
90+
@Override
91+
public boolean nextBoolean() {
92+
return delegate.nextBoolean();
93+
}
94+
95+
@Override
96+
public void nextBytes(byte[] bytes) {
97+
delegate.nextBytes(bytes);
98+
}
99+
}

core/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
exports ai.timefold.solver.core.impl.neighborhood to ai.timefold.solver.enterprise.core;
203203
exports ai.timefold.solver.core.impl.partitionedsearch to ai.timefold.solver.enterprise.core;
204204
exports ai.timefold.solver.core.impl.phase to ai.timefold.solver.enterprise.core;
205+
exports ai.timefold.solver.core.impl.solver.random to ai.timefold.solver.enterprise.core;
205206
exports ai.timefold.solver.core.impl.solver.recaller to ai.timefold.solver.enterprise.core;
206207
exports ai.timefold.solver.core.impl.solver.event to ai.timefold.solver.enterprise.core;
207208

0 commit comments

Comments
 (0)