aboutsummaryrefslogtreecommitdiff
path: root/src/Java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Java')
-rw-r--r--src/Java/gtPlusPlus/core/util/math/MathUtils.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Java/gtPlusPlus/core/util/math/MathUtils.java b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
index a7cb916d0d..96231fb29c 100644
--- a/src/Java/gtPlusPlus/core/util/math/MathUtils.java
+++ b/src/Java/gtPlusPlus/core/util/math/MathUtils.java
@@ -65,6 +65,37 @@ public class MathUtils {
return val;
}
+
+ /**
+ * Returns a psuedo-random number between min and max, inclusive.
+ * The difference between min and max can be at most
+ * Double.MAX_VALUE - 1.
+ *
+ * @param min Minimim value
+ * @param max Maximim value. Must be greater than min.
+ * @return Double between min and max, inclusive.
+ * @see java.util.Random#nextDouble(double)
+ */
+ public static double randDouble(double min, double max) {
+ // Usually this can be a field rather than a method variable
+ Random rand = new Random();
+
+ // nextInt is normally exclusive of the top value,
+ // so add 1 to make it inclusive
+ double randomNum = MathUtils.nextDouble(rand,(max - min) + 1) + min;
+ return randomNum;
+ }
+
+ private static double nextDouble(Random rng, double n) {
+ // error checking and 2^x checking removed for simplicity.
+ double bits, val;
+ do {
+ bits = (rng.nextLong() << 1) >>> 1;
+ val = bits % n;
+ } while (bits-val+(n-1) < 0L);
+ return val;
+ }
+
/**
* Returns a percentage.
@@ -109,6 +140,19 @@ public class MathUtils {
return 5*(Math.round(d/5));
}
+ //Smooth Rounding Function
+ /**
+ * Returns a integer.
+ * The returned number is d rounded to the nearest flat integer.
+ * Supports Doubles as input.
+ *
+ * @param current Current value.
+ * @return integer Rounded value.
+ */
+ public static int roundToClosestInt(double d) {
+ return (int) (Math.round(d * 2) / 2.0);
+ }
+
public static int roundToClosestMultiple(double number, int multiple) {
int result = multiple;
if (number % multiple == 0) {