Fix issues in Random and ThreadLocalRandom (#536)

This commit is contained in:
Ivan Hetman 2020-10-26 12:09:50 +02:00 committed by GitHub
parent baeb2a28a7
commit 605628d6df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -68,6 +68,9 @@ public class TRandom extends TObject implements TSerializable {
} }
public int nextInt(int n) { public int nextInt(int n) {
if (n <= 0) {
throw new IllegalArgumentException();
}
return (int) (nextDouble() * n); return (int) (nextDouble() * n);
} }

View File

@ -15,9 +15,9 @@
*/ */
package org.teavm.classlib.java.util.concurrent; package org.teavm.classlib.java.util.concurrent;
import java.util.Random; import org.teavm.classlib.java.util.TRandom;
public class TThreadLocalRandom extends Random { public class TThreadLocalRandom extends TRandom {
private static final TThreadLocalRandom current = new TThreadLocalRandom(); private static final TThreadLocalRandom current = new TThreadLocalRandom();
private TThreadLocalRandom() { private TThreadLocalRandom() {
@ -37,8 +37,8 @@ public class TThreadLocalRandom extends Random {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
int range = bound - origin; int range = bound - origin;
if (range < 0) { if (range > 0) {
return nextInt(bound - origin) + origin; return nextInt(range) + origin;
} else { } else {
while (true) { while (true) {
int value = nextInt(); int value = nextInt();
@ -67,8 +67,8 @@ public class TThreadLocalRandom extends Random {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
long range = bound - origin; long range = bound - origin;
if (range < 0) { if (range > 0) {
return nextLong(bound - origin) + origin; return nextLong(range) + origin;
} else { } else {
while (true) { while (true) {
long value = nextLong(); long value = nextLong();