v2.4.0: fixed and slightly expanded functionality in Random

This commit is contained in:
Evan Debenham
2024-03-31 15:24:46 -04:00
committed by Evan Debenham
parent a69ca2e15d
commit 97fc33d926

View File

@@ -93,6 +93,11 @@ public class Random {
return min + ((Float(max - min) + Float(max - min))/2f); return min + ((Float(max - min) + Float(max - min))/2f);
} }
//returns a uniformly distributed int in the range [-2^31, 2^31)
public static synchronized int Int() {
return generators.peek().nextInt();
}
//returns a uniformly distributed int in the range [0, max) //returns a uniformly distributed int in the range [0, max)
public static synchronized int Int( int max ) { public static synchronized int Int( int max ) {
return max > 0 ? generators.peek().nextInt(max) : 0; return max > 0 ? generators.peek().nextInt(max) : 0;
@@ -109,20 +114,30 @@ public class Random {
} }
//returns a triangularly distributed int in the range [min, max] //returns a triangularly distributed int in the range [min, max]
//this makes results more likely as they get closer to the middle of the range
public static int NormalIntRange( int min, int max ) { public static int NormalIntRange( int min, int max ) {
return min + (int)((Float() + Float()) * (max - min + 1) / 2f); return min + (int)((Float() + Float()) * (max - min + 1) / 2f);
} }
//returns an inverse triangularly distributed int in the range [min, max]
//this makes results more likely as they get further from the middle of the range
public static int InvNormalIntRange( int min, int max){
float roll1 = Float(), roll2 = Float();
if (Math.abs(roll1-0.5f) >= Math.abs(roll2-0.5f)){
return min + (int)(roll1*(max - min + 1));
} else {
return min + (int)(roll2*(max - min + 1));
}
}
//returns a uniformly distributed long in the range [-2^63, 2^63) //returns a uniformly distributed long in the range [-2^63, 2^63)
public static synchronized long Long() { public static synchronized long Long() {
return generators.peek().nextLong(); return generators.peek().nextLong();
} }
//returns a uniformly distributed long in the range [0, max) //returns a uniformly distributed long in the range [0, max)
public static long Long( long max ) { public static synchronized long Long( long max ) {
long result = Long(); return max > 0 ? generators.peek().nextLong(max) : 0;
if (result < 0) result += Long.MAX_VALUE;
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