Skip to content

Commit

Permalink
Merge pull request #24 from jenetics/issues/PRNGINE-20-lgtm_warnings
Browse files Browse the repository at this point in the history
#20: Fix LGTM warnings.
  • Loading branch information
jenetics committed Jul 22, 2019
2 parents f9fceca + d93a7a1 commit 947750e
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void setSeed(final byte[] seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
setSeed(PRNG.seedBytes(seed, SEED_BYTES));
}

Expand Down
6 changes: 3 additions & 3 deletions prngine/src/main/java/io/jenetics/prngine/KISS64Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ void setSeed(final byte[] seed) {
@Override
public int hashCode() {
int hash = 31;
hash += 37*_x + 17;
hash += 37*_y + 17;
hash += 37*Long.hashCode(_x) + 17;
hash += 37*Long.hashCode(_y) + 17;
hash += 37*_z1 + 17;
hash += 37*_c1 + 17;
hash += 37*_z2 + 17;
Expand Down Expand Up @@ -350,7 +350,7 @@ public void setSeed(final byte[] seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
setSeed(PRNG.seedBytes(seed, SEED_BYTES));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public void setSeed(final byte[] seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
setSeed(PRNG.seedBytes(seed, SEED_BYTES));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private TLRandom(final long seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_sentry != null) {
throw new UnsupportedOperationException(
"The 'setSeed(long)' method is not supported " +
Expand Down Expand Up @@ -328,7 +328,7 @@ public void setSeed(final byte[] seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_state != null) _state.setSeed(seed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private TLRandom(final long seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_sentry != null) {
throw new UnsupportedOperationException(
"The 'setSeed(long)' method is not supported " +
Expand Down Expand Up @@ -331,7 +331,7 @@ public void setSeed(final byte[] seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_state != null) _state.setSeed(seed);
}

Expand Down
51 changes: 22 additions & 29 deletions prngine/src/main/java/io/jenetics/prngine/PRNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,15 @@ public static int nextInt(
}

final int value;

if (origin < bound) {
int n = bound - origin;
if (n > 0) {
value = random.nextInt(n) + origin;
} else {
int r;
do {
r = random.nextInt();
} while (r < origin || r >= bound);
value = r;
}
int n = bound - origin;
if (n > 0) {
value = random.nextInt(n) + origin;
} else {
value = random.nextInt();
int r;
do {
r = random.nextInt();
} while (r < origin || r >= bound);
value = r;
}

return value;
Expand Down Expand Up @@ -230,22 +225,20 @@ public static long nextLong(
}

long value = random.nextLong();
if (origin < bound) {
long n = bound - origin, m = n - 1;
if ((n & m) == 0L) {
value = (value & m) + origin;
} else if (n > 0L) {
for (long u = value >>> 1;
u + m - (value = u % n) < 0L;
u = random.nextLong() >>> 1)
{
}

value += origin;
} else {
while (value < origin || value >= bound) {
value = random.nextLong();
}
long n = bound - origin, m = n - 1;
if ((n & m) == 0L) {
value = (value & m) + origin;
} else if (n > 0L) {
for (long u = value >>> 1;
u + m - (value = u % n) < 0L;
u = random.nextLong() >>> 1)
{
}

value += origin;
} else {
while (value < origin || value >= bound) {
value = random.nextLong();
}
}

Expand Down
2 changes: 1 addition & 1 deletion prngine/src/main/java/io/jenetics/prngine/Random32.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public int nextInt() {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_sentry != null) {
throw new UnsupportedOperationException(
"The 'setSeed(long)' method is not supported."
Expand Down
2 changes: 1 addition & 1 deletion prngine/src/main/java/io/jenetics/prngine/Random64.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public long nextLong() {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
if (_sentry != null) {
throw new UnsupportedOperationException(
"The 'setSeed(long)' method is not supported."
Expand Down
26 changes: 17 additions & 9 deletions prngine/src/main/java/io/jenetics/prngine/XOR32ShiftRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public static enum Shift {
public int shift(int x, final Param param) {
x ^= x << param.a;
x ^= x >>> param.b;
return x^x << param.c;
x ^= x << param.c;
return x ;
}
},

Expand All @@ -108,7 +109,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x << param.c;
x ^= x >>> param.b;
return x^x << param.a;
x ^= x << param.a;
return x;
}
},

Expand All @@ -127,7 +129,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x >>> param.a;
x ^= x << param.b;
return x^x >>> param.c;
x ^= x >>> param.c;
return x;
}
},

Expand All @@ -146,7 +149,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x >>> param.c;
x ^= x << param.b;
return x^x >>> param.a;
x ^= x >>> param.a;
return x;
}
},

Expand All @@ -165,7 +169,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x << param.a;
x ^= x << param.c;
return x^x >>> param.b;
x ^= x >>> param.b;
return x;
}
},

Expand All @@ -184,7 +189,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x << param.c;
x ^= x << param.a;
return x^x >>> param.b;
x ^= x >>> param.b;
return x;
}
},

Expand All @@ -203,7 +209,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x >>> param.a;
x ^= x >>> param.c;
return x^x << param.b;
x ^= x << param.b;
return x;
}
},

Expand All @@ -222,7 +229,8 @@ public int shift(int x, final Param param) {
public int shift(int x, final Param param) {
x ^= x >>> param.c;
x ^= x >>> param.a;
return x^x << param.b;
x ^= x << param.b;
return x;
}
};

Expand Down Expand Up @@ -710,7 +718,7 @@ private static int toSafeSeed(final int seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
_x = toSafeSeed((int)seed);
}

Expand Down
28 changes: 18 additions & 10 deletions prngine/src/main/java/io/jenetics/prngine/XOR64ShiftRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public static enum Shift {
public long shift(long x, final Param param) {
x ^= x << param.a;
x ^= x >>> param.b;
return x^x << param.c;
x ^= x << param.c;
return x;
}
},

Expand All @@ -108,7 +109,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x << param.c;
x ^= x >>> param.b;
return x^x << param.a;
x ^= x << param.a;
return x;
}
},

Expand All @@ -127,7 +129,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x >>> param.a;
x ^= x << param.b;
return x^x >>> param.c;
x ^= x >>> param.c;
return x;
}
},

Expand All @@ -146,7 +149,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x >>> param.c;
x ^= x << param.b;
return x^x >>> param.a;
x ^= x >>> param.a;
return x;
}
},

Expand All @@ -165,7 +169,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x << param.a;
x ^= x << param.c;
return x^x >>> param.b;
x ^= x >>> param.b;
return x;
}
},

Expand All @@ -184,7 +189,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x << param.c;
x ^= x << param.a;
return x^x >>> param.b;
x ^= x >>> param.b;
return x;
}
},

Expand All @@ -203,7 +209,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x >>> param.a;
x ^= x >>> param.c;
return x^x << param.b;
x ^= x << param.b;
return x;
}
},

Expand All @@ -222,7 +229,8 @@ public long shift(long x, final Param param) {
public long shift(long x, final Param param) {
x ^= x >>> param.c;
x ^= x >>> param.a;
return x^x << param.b;
x ^= x << param.b;
return x;
}
};

Expand Down Expand Up @@ -830,7 +838,7 @@ private static long toSafeSeed(final long seed) {
}

@Override
public void setSeed(final long seed) {
public synchronized void setSeed(final long seed) {
_x = toSafeSeed((int)seed);
}

Expand All @@ -847,7 +855,7 @@ public String toString() {
@Override
public int hashCode() {
int hash = 31;
hash += 17*_x + 37;
hash += 17*Long.hashCode(_x) + 37;
hash += 17*_param.hashCode() + 37;

return hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -124,14 +125,15 @@ public static List<Result> test(
randomizerThread.start();

// The dieharder console output.
final BufferedReader stdout = new BufferedReader (
new InputStreamReader(dieharder.getInputStream())
);

final List<Result> results = new ArrayList<>();
for (String l = stdout.readLine(); l != null; l = stdout.readLine()) {
Result.parse(l).ifPresent(results::add);
System.out.println(l);
try (InputStream is = dieharder.getInputStream();
InputStreamReader ir = new InputStreamReader(is);
BufferedReader stdout = new BufferedReader(ir))
{
for (String l = stdout.readLine(); l != null; l = stdout.readLine()) {
Result.parse(l).ifPresent(results::add);
System.out.println(l);
}
}

dieharder.waitFor();
Expand Down

0 comments on commit 947750e

Please sign in to comment.