Add Random next origin, bound methods for float, double, int and long (#609)

This commit is contained in:
Timothy Solum 2022-08-06 06:39:09 -05:00 committed by GitHub
parent 89f41b9a35
commit f7f99b3c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,10 +74,18 @@ public class TRandom extends TObject implements TSerializable {
return (int) (nextDouble() * n); return (int) (nextDouble() * n);
} }
public int nextInt(int origin, int bound) {
return origin + nextInt() * (bound - origin);
}
public long nextLong() { public long nextLong() {
return ((long) nextInt() << 32) | nextInt(); return ((long) nextInt() << 32) | nextInt();
} }
public long nextLong(long origin, long bound) {
return origin + nextLong() * (bound - origin);
}
public boolean nextBoolean() { public boolean nextBoolean() {
return nextInt() % 2 == 0; return nextInt() % 2 == 0;
} }
@ -86,6 +94,10 @@ public class TRandom extends TObject implements TSerializable {
return (float) nextDouble(); return (float) nextDouble();
} }
public float nextFloat(float origin, float bound) {
return origin + nextFloat() * (bound - origin);
}
public double nextDouble() { public double nextDouble() {
if (PlatformDetector.isC()) { if (PlatformDetector.isC()) {
return crand(); 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") @Import(name = "teavm_rand")
@Unmanaged @Unmanaged
private static native double crand(); private static native double crand();