Skip to content

Commit 1144cbc

Browse files
authored
faster-random-generator fix world gen forkPositional seed (#539)
* fix random seed * re * move nextInt * remove impl
1 parent 6144a9a commit 1144cbc

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

leaf-server/src/main/java/org/dreeam/leaf/config/modules/opt/FastRNG.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public String getBasePath() {
1717
public static String randomGenerator = "Xoroshiro128PlusPlus";
1818
public static boolean warnForSlimeChunk = true;
1919
public static boolean useLegacyForSlimeChunk = false;
20+
@Deprecated
2021
public static boolean useDirectImpl = false;
2122

23+
public static boolean worldgen = false;
2224
public static boolean worldgenEnabled() {
23-
return enabled && enableForWorldgen;
25+
return worldgen;
2426
} // Helper function
2527

2628
@Override
@@ -88,5 +90,7 @@ public void onLoaded() {
8890
LeafConfig.LOGGER.warn("Set performance.faster-random-generator.warn-for-slime-chunk to false to " +
8991
"disable this warning.");
9092
}
93+
94+
worldgen = enableForWorldgen && enabled;
9195
}
9296
}

leaf-server/src/main/java/org/dreeam/leaf/util/math/random/FasterRandomSource.java

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,68 @@
55
import net.minecraft.util.RandomSource;
66
import net.minecraft.world.level.levelgen.BitRandomSource;
77
import net.minecraft.world.level.levelgen.PositionalRandomFactory;
8+
import net.minecraft.world.level.levelgen.RandomSupport;
89
import org.dreeam.leaf.config.modules.opt.FastRNG;
10+
import org.jspecify.annotations.NullMarked;
911

10-
import java.util.concurrent.ThreadLocalRandom;
1112
import java.util.random.RandomGenerator;
1213
import java.util.random.RandomGeneratorFactory;
1314

14-
public class FasterRandomSource implements BitRandomSource {
15+
@NullMarked
16+
public final class FasterRandomSource implements BitRandomSource {
1517

1618
private static final int INT_BITS = 48;
1719
private static final long SEED_MASK = 0xFFFFFFFFFFFFL;
1820
private static final long MULTIPLIER = 25214903917L;
1921
private static final long INCREMENT = 11L;
2022
private static final RandomGeneratorFactory<RandomGenerator> RANDOM_GENERATOR_FACTORY = RandomGeneratorFactory.of(FastRNG.randomGenerator);
21-
private static final boolean isSplittableGenerator = RANDOM_GENERATOR_FACTORY.isSplittable();
23+
private RandomGenerator delegate;
24+
@Deprecated
2225
private long seed;
26+
public static final FasterRandomSource SHARED_INSTANCE = new FasterRandomSource(RandomSupport.generateUniqueSeed());
27+
@Deprecated
2328
private static final boolean useDirectImpl = FastRNG.useDirectImpl;
24-
private RandomGenerator randomGenerator;
25-
public static final FasterRandomSource SHARED_INSTANCE = new FasterRandomSource(ThreadLocalRandom.current().nextLong());
2629

2730
public FasterRandomSource(long seed) {
2831
this.seed = seed;
29-
this.randomGenerator = RANDOM_GENERATOR_FACTORY.create(seed);
32+
this.delegate = RANDOM_GENERATOR_FACTORY.create(seed);
3033
}
3134

32-
private FasterRandomSource(long seed, RandomGenerator.SplittableGenerator randomGenerator) {
33-
this.seed = seed;
34-
this.randomGenerator = randomGenerator;
35+
private FasterRandomSource(RandomGenerator.SplittableGenerator randomGenerator) {
36+
this.seed = randomGenerator.nextLong();
37+
this.delegate = randomGenerator;
3538
}
3639

3740
@Override
38-
public final RandomSource fork() {
39-
if (isSplittableGenerator) {
40-
return new FasterRandomSource(seed, ((RandomGenerator.SplittableGenerator) this.randomGenerator).split());
41+
public RandomSource fork() {
42+
if (useDirectImpl) {
43+
return new FasterRandomSource(this.nextLong());
4144
}
42-
43-
return new FasterRandomSource(this.nextLong());
45+
return RANDOM_GENERATOR_FACTORY.isSplittable()
46+
? new FasterRandomSource(((RandomGenerator.SplittableGenerator) this.delegate).split())
47+
: new FasterRandomSource(this.nextLong());
4448
}
4549

4650
@Override
47-
public final PositionalRandomFactory forkPositional() {
48-
return new FasterRandomSourcePositionalRandomFactory(this.seed);
51+
public PositionalRandomFactory forkPositional() {
52+
return new FasterRandomSourcePositionalRandomFactory(this.nextLong());
4953
}
5054

5155
@Override
52-
public final void setSeed(long seed) {
56+
public void setSeed(long seed) {
5357
this.seed = seed;
54-
this.randomGenerator = RANDOM_GENERATOR_FACTORY.create(seed);
58+
this.delegate = RANDOM_GENERATOR_FACTORY.create(seed);
5559
}
5660

5761
@Override
58-
public final int next(int bits) {
59-
return (int) ((seed = seed * MULTIPLIER + INCREMENT & SEED_MASK) >>> (INT_BITS - bits));
62+
public int next(int bits) {
63+
if (useDirectImpl) {
64+
return (int) ((seed = seed * MULTIPLIER + INCREMENT & SEED_MASK) >>> (INT_BITS - bits));
65+
}
66+
return (int) (nextLong() >>> (64 - bits));
6067
}
6168

62-
public static class FasterRandomSourcePositionalRandomFactory implements PositionalRandomFactory {
69+
private static final class FasterRandomSourcePositionalRandomFactory implements PositionalRandomFactory {
6370
private final long seed;
6471

6572
public FasterRandomSourcePositionalRandomFactory(long seed) {
@@ -68,9 +75,7 @@ public FasterRandomSourcePositionalRandomFactory(long seed) {
6875

6976
@Override
7077
public RandomSource at(int x, int y, int z) {
71-
long l = Mth.getSeed(x, y, z);
72-
long m = l ^ this.seed;
73-
return new FasterRandomSource(m);
78+
return new FasterRandomSource(Mth.getSeed(x, y, z) ^ this.seed);
7479
}
7580

7681
@Override
@@ -92,17 +97,16 @@ public void parityConfigString(StringBuilder info) {
9297
}
9398

9499
@Override
95-
public final int nextInt() {
100+
public int nextInt() {
96101
if (useDirectImpl) {
97102
return (int) (((seed = seed * MULTIPLIER + INCREMENT & SEED_MASK) >>> 16) ^
98103
((seed = seed * MULTIPLIER + INCREMENT & SEED_MASK) >>> 32));
99104
}
100-
101-
return randomGenerator.nextInt();
105+
return delegate.nextInt();
102106
}
103107

104108
@Override
105-
public final int nextInt(int bound) {
109+
public int nextInt(int bound) {
106110
if (useDirectImpl && bound > 0) {
107111
if ((bound & -bound) == bound) {
108112
return (int) ((bound * (long) next(31)) >> 31);
@@ -114,50 +118,53 @@ public final int nextInt(int bound) {
114118
} while (bits - val + (bound - 1) < 0);
115119
return val;
116120
}
121+
return delegate.nextInt(bound);
122+
}
117123

118-
return randomGenerator.nextInt(bound);
124+
@Override
125+
public int nextInt(int origin, int bound) {
126+
if (useDirectImpl && bound > 0) {
127+
return origin + this.nextInt(bound - origin);
128+
}
129+
return delegate.nextInt(origin, bound);
119130
}
120131

121132
@Override
122-
public final long nextLong() {
133+
public long nextLong() {
123134
if (useDirectImpl) {
124135
return ((long) next(32) << 32) + next(32);
125136
}
126-
127-
return randomGenerator.nextLong();
137+
return delegate.nextLong();
128138
}
129139

130140
@Override
131-
public final boolean nextBoolean() {
141+
public boolean nextBoolean() {
132142
if (useDirectImpl) {
133143
return next(1) != 0;
134144
}
135-
136-
return randomGenerator.nextBoolean();
145+
return delegate.nextBoolean();
137146
}
138147

139148
@Override
140-
public final float nextFloat() {
149+
public float nextFloat() {
141150
if (useDirectImpl) {
142151
return next(24) / ((float) (1 << 24));
143152
}
144-
145-
return randomGenerator.nextFloat();
153+
return delegate.nextFloat();
146154
}
147155

148156
@Override
149-
public final double nextDouble() {
157+
public double nextDouble() {
150158
if (useDirectImpl) {
151159
return (((long) next(26) << 27) + next(27)) / (double) (1L << 53);
152160
}
153-
154-
return randomGenerator.nextDouble();
161+
return delegate.nextDouble();
155162
}
156163

157164
@Override
158-
public final double nextGaussian() {
165+
public double nextGaussian() {
159166
// delegate Gaussian distribution to RandomGenerator
160167
// as direct implementation would be complex (i aint doin allat)
161-
return randomGenerator.nextGaussian();
168+
return delegate.nextGaussian();
162169
}
163170
}

0 commit comments

Comments
 (0)