Allows running tests using a seeded RNG.

Running the same game twice now works! There may be issues I haven't found with certain AI behaviors around mechanics I didn't use in my tests.


(cherry picked from commit 194b47c1ad61c8f1efb6bce8af2bb10d1fa8f6c3)
This commit is contained in:
Meerkov
2018-04-18 20:57:01 -07:00
parent 38376b3978
commit bd097888a3
5 changed files with 36 additions and 19 deletions

View File

@@ -34,22 +34,27 @@ public class MyRandom {
private static Random random = new SecureRandom();
/**
* <p>
* percentTrue.<br>
* If percent is like 30, then 30% of the time it will be true.
* </p>
*
* @param percent
* a int.
* @return a boolean.
* Changes into a non-CSPRNG, which can be seeded for use in tests/repeatable experiments.
*/
public static boolean percentTrue(final int percent) {
return percent > MyRandom.getRandom().nextInt(100);
public static void setSeed(int seed) {
System.out.println("Setting the RNG seed to: " + seed);
random = new Random(seed);
}
/**
* Returns True with Percent probability.
*
* TODO: My guess is no one is passing in a number scaled to 100. This API should probably be cut.
*/
public static boolean percentTrue(final long percent) {
return percent > MyRandom.getRandom().nextDouble() * 100;
}
/**
* Gets the random.
*
* TODO: Make this private, and instead add a robust set of APIs here.
*
* @return the random
*/
public static Random getRandom() {
@@ -60,7 +65,7 @@ public class MyRandom {
int[] groups = new int[numGroups];
for (int i = 0; i < value; i++) {
groups[random.nextInt(numGroups)]++;
groups[MyRandom.getRandom().nextInt(numGroups)]++;
}
return groups;