Implement some of the missing methods in ThreadLocalRandom

This commit is contained in:
Alexey Andreev 2020-04-14 14:03:15 +03:00
parent 061bb859d5
commit 2bc9329cf8

View File

@ -28,7 +28,72 @@ public class TThreadLocalRandom extends Random {
} }
@Override @Override
public synchronized void setSeed(long seed) { public void setSeed(long seed) {
throw new UnsupportedOperationException(); 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);
}
} }