From 2d73403b9a3dc28250bc75595a07b2b23aa9473f Mon Sep 17 00:00:00 2001 From: iamdudeman Date: Sat, 13 Aug 2022 11:38:38 -0500 Subject: [PATCH] Moved next origin/bound implementations from TThreadLocalRandom to TRandom. --- .../org/teavm/classlib/java/util/TRandom.java | 75 +++++++++++++++---- .../util/concurrent/TThreadLocalRandom.java | 65 ---------------- 2 files changed, 61 insertions(+), 79 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 30157cdae..aeaca755f 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 @@ -75,15 +75,54 @@ public class TRandom extends TObject implements TSerializable { } public int nextInt(int origin, int bound) { - return origin + nextInt() * (bound - origin); + if (origin >= bound) { + throw new IllegalArgumentException(); + } + int range = bound - origin; + if (range > 0) { + return nextInt(range) + origin; + } else { + while (true) { + int value = nextInt(); + if (value >= origin && value < bound) { + return value; + } + } + } } public long nextLong() { return ((long) nextInt() << 32) | nextInt(); } + public long nextLong(long bound) { + if (bound <= 0) { + throw new IllegalArgumentException(); + } + while (true) { + long value = nextLong(); + long result = value % bound; + if (value - result + (bound - 1) < 0) { + return result; + } + } + } + public long nextLong(long origin, long bound) { - return origin + nextLong() * (bound - origin); + if (origin >= bound) { + throw new IllegalArgumentException(); + } + long range = bound - origin; + if (range > 0) { + return nextLong(range) + origin; + } else { + while (true) { + long value = nextLong(); + if (value >= origin && value < bound) { + return value; + } + } + } } public boolean nextBoolean() { @@ -94,8 +133,12 @@ public class TRandom extends TObject implements TSerializable { return (float) nextDouble(); } + public float nextFloat(float bound) { + return (float) nextDouble(bound); + } + public float nextFloat(float origin, float bound) { - return origin + nextFloat() * (bound - origin); + return (float) nextDouble(origin, bound); } public double nextDouble() { @@ -106,8 +149,22 @@ public class TRandom extends TObject implements TSerializable { } } + public double nextDouble(double bound) { + if (bound <= 0) { + throw new IllegalArgumentException(); + } + double value = nextDouble() * bound; + if (value == bound) { + value = Math.nextDown(value); + } + return value; + } + public double nextDouble(double origin, double bound) { - return origin + nextDouble() * (bound - origin); + if (origin >= bound) { + throw new IllegalArgumentException(); + } + return origin + nextDouble(bound - origin); } @Import(name = "teavm_rand") @@ -294,16 +351,6 @@ public class TRandom extends TObject implements TSerializable { }; } - private long nextLong(long limit) { - while (true) { - long value = nextLong(); - long result = value % limit; - if (value - result + (limit - 1) < 0) { - return result; - } - } - } - public TLongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) { if (streamSize < 0 || randomNumberOrigin >= randomNumberBound) { throw new IllegalArgumentException(); 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 94554e5e6..72b1bface 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 @@ -31,69 +31,4 @@ public class TThreadLocalRandom extends TRandom { public void setSeed(long seed) { throw new UnsupportedOperationException(); } - - public int nextInt(int origin, int bound) { - if (origin >= bound) { - throw new IllegalArgumentException(); - } - int range = bound - origin; - if (range > 0) { - return nextInt(range) + origin; - } else { - while (true) { - int value = nextInt(); - if (value >= origin && value < bound) { - return value; - } - } - } - } - - public long nextLong(long bound) { - if (bound <= 0) { - throw new IllegalArgumentException(); - } - while (true) { - long value = nextLong(); - long result = value % bound; - if (value - result + (bound - 1) < 0) { - return result; - } - } - } - - public long nextLong(long origin, long bound) { - if (origin >= bound) { - throw new IllegalArgumentException(); - } - long range = bound - origin; - if (range > 0) { - return nextLong(range) + origin; - } else { - while (true) { - long value = nextLong(); - if (value >= origin && value < bound) { - return value; - } - } - } - } - - public double nextDouble(double bound) { - if (bound <= 0) { - throw new IllegalArgumentException(); - } - double value = nextDouble() * bound; - if (value == bound) { - value = Math.nextDown(value); - } - return value; - } - - public double nextDouble(double origin, double bound) { - if (origin >= bound) { - throw new IllegalArgumentException(); - } - return origin + nextDouble(bound - origin); - } }