v2.4.0: fixed and slightly expanded functionality in Random
This commit is contained in:
committed by
Evan Debenham
parent
a69ca2e15d
commit
97fc33d926
@@ -93,6 +93,11 @@ public class Random {
|
||||
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)
|
||||
public static synchronized int Int( int max ) {
|
||||
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]
|
||||
//this makes results more likely as they get closer to the middle of the range
|
||||
public static int NormalIntRange( int min, int max ) {
|
||||
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)
|
||||
public static synchronized long Long() {
|
||||
return generators.peek().nextLong();
|
||||
}
|
||||
|
||||
//returns a uniformly distributed long in the range [0, max)
|
||||
public static long Long( long max ) {
|
||||
long result = Long();
|
||||
if (result < 0) result += Long.MAX_VALUE;
|
||||
return result % max;
|
||||
public static synchronized long Long( long max ) {
|
||||
return max > 0 ? generators.peek().nextLong(max) : 0;
|
||||
}
|
||||
|
||||
//returns an index from chances, the probability of each index is the weight values in changes
|
||||
|
||||
Reference in New Issue
Block a user