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) {
if (n <= 0) {
throw new IllegalArgumentException();
}
return (int) (nextDouble() * n);
}

View File

@ -15,9 +15,9 @@
*/
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 TThreadLocalRandom() {
@ -37,8 +37,8 @@ public class TThreadLocalRandom extends Random {
throw new IllegalArgumentException();
}
int range = bound - origin;
if (range < 0) {
return nextInt(bound - origin) + origin;
if (range > 0) {
return nextInt(range) + origin;
} else {
while (true) {
int value = nextInt();
@ -67,8 +67,8 @@ public class TThreadLocalRandom extends Random {
throw new IllegalArgumentException();
}
long range = bound - origin;
if (range < 0) {
return nextLong(bound - origin) + origin;
if (range > 0) {
return nextLong(range) + origin;
} else {
while (true) {
long value = nextLong();