Skip to content

Commit

Permalink
Move mathematical logic to record RandomGeneratorProperties.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgiulietti committed May 14, 2024
1 parent 6179b5e commit e8432cc
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private static Map<String, RandomGeneratorProperties> createFactoryMap() {

private RandomGenerator create() {
return switch (name) {
case "SecureRandom" -> new SecureRandom();
case "Random" -> new Random();
case "Random" -> new Random();
case "SecureRandom" -> new SecureRandom();
case "SplittableRandom" -> new SplittableRandom();
case "L32X64MixRandom" -> new L32X64MixRandom();
case "L64X128MixRandom" -> new L64X128MixRandom();
Expand All @@ -230,7 +230,7 @@ private RandomGenerator create(long seed) {
+ name + " does not support a long seed");
}
return switch (name) {
case "Random" -> new Random(seed);
case "Random" -> new Random(seed);
case "SplittableRandom" -> new SplittableRandom(seed);
case "L32X64MixRandom" -> new L32X64MixRandom(seed);
case "L64X128MixRandom" -> new L64X128MixRandom(seed);
Expand Down Expand Up @@ -282,6 +282,20 @@ private boolean isInstantiable() {
private boolean isDeprecated() {
return (flags & DEPRECATED) != 0;
}

private BigInteger period() {
/*
* 0 if i = j = k = 0
* (2^i - j) 2^k otherwise
*/
return i == 0 && j == 0 && k == 0
? BigInteger.ZERO
: BigInteger.ONE.shiftLeft(i).subtract(BigInteger.valueOf(j)).shiftLeft(k);
}

private int stateBits() {
return i == 0 && k == 0 ? Integer.MAX_VALUE : i + k;
}
}

/**
Expand Down Expand Up @@ -472,10 +486,7 @@ public String group() {
* to maintain state of seed.
*/
public int stateBits() {
int i = properties.i();
int k = properties.k();

return i == 0 && k == 0 ? Integer.MAX_VALUE : i + k;
return properties.stateBits();
}

/**
Expand All @@ -495,13 +506,7 @@ public int equidistribution() {
* @return BigInteger period.
*/
public BigInteger period() {
int i = properties.i();
int j = properties.j();
int k = properties.k();

return i == 0 && j == 0 && k == 0
? BigInteger.ZERO
: BigInteger.ONE.shiftLeft(i).subtract(BigInteger.valueOf(j)).shiftLeft(k);
return properties.period();
}

/**
Expand Down

0 comments on commit e8432cc

Please sign in to comment.