Skip to content

Commit 4f08269

Browse files
committed
wasm gc friendly rng
1 parent fd7d6ef commit 4f08269

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/main/java/net/kite/internal/demo/KiteDemo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ public void onStart() {
505505

506506
byte[] bytes = array.copyToJavaArray();
507507
OpeningBoardScoreCaches.ensureDefaultIsLoaded(bytes);
508+
net.kite.internal.util.random.Random.setUseSimpleStartSeedEntropy();
508509

509510
buildApp();
510511

src/main/java/net/kite/internal/util/random/Random.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public final class Random {
2929
private static final float RANDOM_FLOAT_MULTIPLIER = 0x1.0p-24F;
3030
private static final double RANDOM_DOUBLE_MULTIPLIER = 0x1.0p-53;
3131

32+
private static boolean useSimpleStartSeedEntropy;
33+
3234
private long currentSeed;
3335

3436
public Random() {
@@ -46,18 +48,25 @@ public long getSeed() {
4648
}
4749

4850
public long setRandomSeed() {
49-
System.out.println("before curr thread");
5051
Thread currentThread = Thread.currentThread();
51-
System.out.println("current thread: " + currentThread);
5252
ThreadLocalRandom currentThreadRandom = ThreadLocalRandom.current();
53-
System.out.println(currentThreadRandom);
5453

55-
long l1 = System.nanoTime();
56-
@SuppressWarnings("deprecation")
57-
long l2 = currentThread.getId() * START_SEED_THREAD_ID_MULTIPLIER;
58-
long l3 = currentThreadRandom.nextLong();
54+
long startSeed;
55+
56+
if(useSimpleStartSeedEntropy) {
57+
58+
startSeed = System.nanoTime();
59+
60+
} else {
61+
62+
long l1 = System.nanoTime();
63+
@SuppressWarnings("deprecation")
64+
long l2 = currentThread.getId() * START_SEED_THREAD_ID_MULTIPLIER;
65+
long l3 = currentThreadRandom.nextLong();
66+
67+
startSeed = l1 ^ l2 ^ l3;
68+
}
5969

60-
long startSeed = l1 ^ l2 ^ l3;
6170
setSeed(startSeed);
6271

6372
return startSeed;
@@ -144,4 +153,8 @@ private static long scrambleSeed(long seed) {
144153
return seed;
145154
}
146155

156+
public static void setUseSimpleStartSeedEntropy() {
157+
useSimpleStartSeedEntropy = true;
158+
}
159+
147160
}

0 commit comments

Comments
 (0)