diff options
author | draknyte1 <draknyte1@hotmail.com> | 2017-02-15 22:43:41 +1000 |
---|---|---|
committer | draknyte1 <draknyte1@hotmail.com> | 2017-02-15 22:43:41 +1000 |
commit | 6838197b7329652ec37b8dc443e0054568a6772e (patch) | |
tree | 34291908db04596fb60ed98a34c7f8100a26cd22 /src/Java | |
parent | 35967f43703362f06330aefac4ba06dde5057527 (diff) | |
download | GT5-Unofficial-6838197b7329652ec37b8dc443e0054568a6772e.tar.gz GT5-Unofficial-6838197b7329652ec37b8dc443e0054568a6772e.tar.bz2 GT5-Unofficial-6838197b7329652ec37b8dc443e0054568a6772e.zip |
% More work on LFTR Sparging.
Diffstat (limited to 'src/Java')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/math/MathUtils.java | 44 |
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) { |