v2.4.0: fixed Random.Long accidentally using Java 17+ APIs

This commit is contained in:
Evan Debenham
2024-04-20 13:31:56 -04:00
committed by Evan Debenham
parent 5b959f315c
commit 32a1e46311

View File

@@ -135,9 +135,12 @@ public class Random {
return generators.peek().nextLong(); return generators.peek().nextLong();
} }
//returns a uniformly distributed long in the range [0, max) //returns a mostly uniformly distributed long in the range [0, max)
public static synchronized long Long( long max ) { public static long Long( long max ) {
return max > 0 ? generators.peek().nextLong(max) : 0; long result = Long();
if (result < 0) result += Long.MAX_VALUE;
//modulo isn't perfect, but as long as max is reasonably below 2^63 it's close enough
return result % max;
} }
//returns an index from chances, the probability of each index is the weight values in changes //returns an index from chances, the probability of each index is the weight values in changes