-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: use L128X1024Mix for random generator
fixes #363
- Loading branch information
Vladimir Sitnikov
committed
Dec 10, 2022
1 parent
7a37d8d
commit 75a0edb
Showing
146 changed files
with
1,033 additions
and
844 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package net.jqwik.api; | ||
|
||
import net.jqwik.api.random.*; | ||
|
||
import org.apache.commons.rng.*; | ||
|
||
import java.util.*; | ||
|
||
public interface JqwikRandom extends UniformRandomProvider { | ||
JqwikRandom jump(); | ||
|
||
default JqwikRandom split() { | ||
return split(this); | ||
} | ||
|
||
JqwikRandom split(UniformRandomProvider source); | ||
|
||
JqwikRandomState saveState(); | ||
|
||
void restoreState(JqwikRandomState state); | ||
|
||
default Random asJdkRandom() { | ||
return new Random() { | ||
@Override | ||
protected int next(int bits) { | ||
int next = JqwikRandom.this.nextInt(); | ||
next &= ((1L << bits) - 1); | ||
return next; | ||
} | ||
|
||
@Override | ||
public long nextLong() { | ||
return JqwikRandom.this.nextLong(); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package net.jqwik.api.random; | ||
|
||
public interface JqwikRandomSeed { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package net.jqwik.api.random; | ||
|
||
public interface JqwikRandomState { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 39 additions & 61 deletions
100
engine/src/main/java/net/jqwik/engine/SourceOfRandomness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,69 @@ | ||
package net.jqwik.engine; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.function.*; | ||
import java.math.*; | ||
import java.security.*; | ||
|
||
import net.jqwik.api.*; | ||
|
||
import net.jqwik.api.random.*; | ||
|
||
import net.jqwik.engine.random.*; | ||
|
||
import org.apache.commons.rng.*; | ||
import org.apache.commons.rng.core.*; | ||
import org.apache.commons.rng.core.source64.*; | ||
|
||
public class SourceOfRandomness { | ||
|
||
private SourceOfRandomness() { | ||
} | ||
|
||
private static final Supplier<Random> RNG = ThreadLocalRandom::current; | ||
private static final L128X1024Mix RNG = new L128X1024Mix(new SecureRandom().longs().limit(20).toArray()); | ||
|
||
private static final ThreadLocal<Random> current = ThreadLocal.withInitial(SourceOfRandomness::newRandom); | ||
private static final ThreadLocal<JqwikRandom> current = ThreadLocal.withInitial(SourceOfRandomness::newRandom); | ||
|
||
public static String encodeSeed(RandomProviderState state) { | ||
return new BigInteger(((RandomProviderDefaultState) state).getState()).toString(36); | ||
} | ||
|
||
public static RandomProviderState decodeSeed(String seed) { | ||
return new RandomProviderDefaultState(new BigInteger(seed, 36).toByteArray()); | ||
} | ||
|
||
public static String createRandomSeed() { | ||
return Long.toString(RNG.get().nextLong()); | ||
synchronized (RNG) { | ||
RNG.jump(); | ||
return encodeSeed(RNG.saveState()); | ||
} | ||
} | ||
|
||
public static Random create(String seed) { | ||
public static JqwikRandom create(String seed) { | ||
try { | ||
Random random = newRandom(Long.parseLong(seed)); | ||
L128X1024Mix core = new L128X1024Mix(new long[0]); | ||
core.restoreState(decodeSeed(seed)); | ||
JqwikRandom random = new JqwikRandomImpl(core); | ||
current.set(random); | ||
return random; | ||
} catch (NumberFormatException nfe) { | ||
throw new JqwikException(String.format("[%s] is not a valid random seed.", seed)); | ||
} | ||
} | ||
|
||
public static Random newRandom() { | ||
return new XORShiftRandom(); | ||
public static JqwikRandom newRandom() { | ||
return new JqwikRandomImpl(new L128X1024Mix(new long[]{ System.nanoTime()})); | ||
} | ||
|
||
public static Random newRandom(final long seed) { | ||
return new XORShiftRandom(seed); | ||
@Deprecated | ||
public static JqwikRandom newRandom(final long seed) { | ||
return new JqwikRandomImpl(new L128X1024Mix(new long[]{seed})); | ||
} | ||
|
||
public static Random current() { | ||
return current.get(); | ||
public static JqwikRandom newRandom(final JqwikRandomState seed) { | ||
JqwikRandomImpl random = new JqwikRandomImpl(new L128X1024Mix(new long[]{1})); | ||
random.restoreState(seed); | ||
return random; | ||
} | ||
|
||
/** | ||
* A faster but not thread safe implementation of {@linkplain java.util.Random}. | ||
* It also has a period of 2^n - 1 and better statistical randomness. | ||
* | ||
* See for details: https://www.javamex.com/tutorials/random_numbers/xorshift.shtml | ||
* | ||
* <p> | ||
* For further performance improvements within jqwik, consider to override: | ||
* <ul> | ||
* <li>nextDouble()</li> | ||
* <li>nextBytes(int)</li> | ||
* </ul> | ||
*/ | ||
private static class XORShiftRandom extends Random { | ||
private long seed; | ||
|
||
private XORShiftRandom() { | ||
this(System.nanoTime()); | ||
} | ||
|
||
private XORShiftRandom(long seed) { | ||
if (seed == 0l) { | ||
throw new IllegalArgumentException("0L is not an allowed seed value"); | ||
} | ||
this.seed = seed; | ||
} | ||
|
||
@Override | ||
protected int next(int nbits) { | ||
long x = nextLong(); | ||
x &= ((1L << nbits) - 1); | ||
return (int) x; | ||
} | ||
|
||
/** | ||
* Will never generate 0L | ||
*/ | ||
@Override | ||
public long nextLong() { | ||
long x = this.seed; | ||
x ^= (x << 21); | ||
x ^= (x >>> 35); | ||
x ^= (x << 4); | ||
this.seed = x; | ||
return x; | ||
} | ||
public static JqwikRandom current() { | ||
return current.get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.