From 605628d6dffdd26c61a44c047fe699faabe017de Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Mon, 26 Oct 2020 12:09:50 +0200 Subject: [PATCH] Fix issues in Random and ThreadLocalRandom (#536) --- .../java/org/teavm/classlib/java/util/TRandom.java | 3 +++ .../java/util/concurrent/TThreadLocalRandom.java | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java b/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java index da18c4683..f72bbfbe2 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java @@ -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); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java index 54427c8f4..94554e5e6 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java @@ -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();