From f7f99b3c9f56d926176530811ce4925fa2e079b4 Mon Sep 17 00:00:00 2001 From: Timothy Solum Date: Sat, 6 Aug 2022 06:39:09 -0500 Subject: [PATCH] Add Random next origin, bound methods for float, double, int and long (#609) --- .../org/teavm/classlib/java/util/TRandom.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 f72bbfbe2..30157cdae 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 @@ -74,10 +74,18 @@ public class TRandom extends TObject implements TSerializable { return (int) (nextDouble() * n); } + public int nextInt(int origin, int bound) { + return origin + nextInt() * (bound - origin); + } + public long nextLong() { return ((long) nextInt() << 32) | nextInt(); } + public long nextLong(long origin, long bound) { + return origin + nextLong() * (bound - origin); + } + public boolean nextBoolean() { return nextInt() % 2 == 0; } @@ -86,6 +94,10 @@ public class TRandom extends TObject implements TSerializable { return (float) nextDouble(); } + public float nextFloat(float origin, float bound) { + return origin + nextFloat() * (bound - origin); + } + public double nextDouble() { if (PlatformDetector.isC()) { return crand(); @@ -94,6 +106,10 @@ public class TRandom extends TObject implements TSerializable { } } + public double nextDouble(double origin, double bound) { + return origin + nextDouble() * (bound - origin); + } + @Import(name = "teavm_rand") @Unmanaged private static native double crand();