From 2bc9329cf807d897f59f52d296bd6cd8fc0c6b57 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 14 Apr 2020 14:03:15 +0300 Subject: [PATCH] Implement some of the missing methods in ThreadLocalRandom --- .../util/concurrent/TThreadLocalRandom.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) 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 57134cab8..54427c8f4 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 @@ -28,7 +28,72 @@ public class TThreadLocalRandom extends Random { } @Override - public synchronized void setSeed(long seed) { + 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(bound - origin) + 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(bound - origin) + 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); + } }