-
Notifications
You must be signed in to change notification settings - Fork 3
Maths
These methods are used to generate a random number from the specified range. The generated value is greater than or equal to first parameter and less than or equal to second parameter.
public static byte random(byte lowerRange, byte upperRange);
public static byte random(byte lowerRange, byte upperRange, Generator generator);
public static short random(short lowerRange, short upperRange);
public static short random(short lowerRange, short upperRange, Generator generator);
public static int random(int lowerRange, int upperRange);
public static int random(int lowerRange, int upperRange, Generator generator);
public static long random(long lowerRange, long upperRange);
public static long random(long lowerRange, long upperRange, Generator generator);
public static float random(float lowerRange, float upperRange);
public static float random(float lowerRange, float upperRange, Generator generator);
public static double random(double lowerRange, double upperRange);
public static double random(double lowerRange, double upperRange, Generator generator);
As you can see in the above example, the methods take Generator. Thanks to this, when you write a test you can use mocked generator that will always return the same value. You can also write your own generator for any distribution. Simply write your own class that implements the random method from abstract class Generator.
public abstract class Generator {
public abstract double random();
public final double generate() {
double randomValue = random();
if (randomValue < 0 || randomValue >= 1) {
throw new InvalidArgumentException("Generated value cannot be less than 0 and greater than or equal to 1");
}
return randomValue;
}
}
If you want to calculate the distance between two points in 2D and 3D, it's the best to use distance method.
public static double distance(int x1, int y1, int x2, int y2);
public static double distance(int x1, int y1, int z1, int x2, int y2, int z2);
public static double distance(double x1, double y1, double x2, double y2);
public static double distance(double x1, double y1, double z1, double x2, double y2, double z2);
public static double distance(Point2D p1, Point2D p2);
public static double distance(Point3D p1, Point3D p2);
public class Point2D {
private final double x;
private final double y;
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
public class Point3D {
private final double x;
private final double y;
private final double z;
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
}
When you try to parse the string to a number, you can come across on four cases:
- value is correct number
- value is incorrect number
- value is less than minimum value of given number
- value is greater than maximum value of given number
In order to have full control over the process of parsing, was created ParseContext:
- TRY_ADJUST : Default context
- value is incorrect => 0
- value is less than minimum => MIN_VALUE
- value is greater than maximum => MAX_VALUE
- ALWAYS_ZERO :
- value is incorrect => 0
- value is less than minimum => 0
- value is greater than maximum => 0
- ZERO_WHEN_INCORRECT :
- value is incorrect => 0
- value is less than minimum => OutOfRangeException is thrown with Range equals to MIN
- value is greater than maximum => OutOfRangeException is thrown with Range equals to MAX
- EXCEPTION :
- value is incorrect => ParseException is thrown
- value is less than minimum => OutOfRangeException is thrown with Range equals to MIN
- value is greater than maximum => OutOfRangeException is thrown with Range equals to MAX
public static byte parseByte(String value);
public static byte parseByte(String value, ParseContext context);
public static short parseShort(String value);
public static short parseShort(String value, ParseContext context);
public static int parseInteger(String value);
public static int parseInteger(String value, ParseContext context);
public static long parseLong(String value);
public static long parseLong(String value, ParseContext context);
public static float parseFloat(String value);
public static float parseFloat(String value, ParseContext context);
public static double parseDouble(String value);
public static double parseDouble(String value, ParseContext context);
If you want to normalize the given value to the interval <0, 1>, simply use normalize:
public static double normalize(byte value, byte minValue, byte maxValue);
public static double normalize(short value, short minValue, short maxValue);
public static double normalize(int value, int minValue, int maxValue);
public static double normalize(long value, long minValue, long maxValue)
public static double normalize(float value, float minValue, float maxValue);
public static double normalize(double value, double minValue, double maxValue);
You can also normalize collection and array of number:
public static Collection<Double> normalizeByte(Collection<Byte> values);
public static Collection<Double> normalizeShort(Collection<Short> values);
public static Collection<Double> normalizeInt(Collection<Integer> values);
public static Collection<Double> normalizeLong(Collection<Long> values);
public static Collection<Double> normalizeFloat(Collection<Float> values);
public static Collection<Double> normalizeDouble(Collection<Double> values);
public static Collection<Double> normalizeByte(Byte... values);
public static Collection<Double> normalizeShort(Short... values);
public static Collection<Double> normalizeInt(Integer... values);
public static Collection<Double> normalizeLong(Long... values);
public static Collection<Double> normalizeFloat(Float... values);
public static List<Double> normalizeDouble(Double... values);
If you want find minimal and maximal value, simply use minMax methods:
public static MinMaxValue<Byte> minMaxByte(Collection<Byte> values);
public static MinMaxValue<Short> minMaxShort(Collection<Short> values);
public static MinMaxValue<Integer> minMaxInteger(Collection<Integer> values);
public static MinMaxValue<Long> minMaxLong(Collection<Long> values);
public static MinMaxValue<Float> minMaxFloat(Collection<Float> values);
public static MinMaxValue<Double> minMaxDouble(Collection<Double> values);
public static MinMaxValue<Byte> minMaxByte(Byte... values);
public static MinMaxValue<Short> minMaxShort(Short... values);
public static MinMaxValue<Integer> minMaxInteger(Integer... values);
public static MinMaxValue<Long> minMaxLong(Long... values);
public static MinMaxValue<Float> minMaxFloat(Float... values);
public static MinMaxValue<Double> minMaxDouble(Double... values);
This method works as follows:
- If the value is in the range, this value is returned
- If the value is less than lower range, the lower range is returned
- If the value is greater than upper range, the upper range is returned
public static byte adjustToRange(byte value, byte min, byte max);
public static short adjustToRange(short value, short min, short max);
public static int adjustToRange(int value, int min, int max);
public static long adjustToRange(long value, long min, long max);
public static float adjustToRange(float value, float min, float max);
public static double adjustToRange(double value, double min, double max);
If you want to calculate average value of collection or array, simply use:
public static double averageByte(Collection<Byte> values);
public static double averageShort(Collection<Short> values);
public static double averageInteger(Collection<Integer> values);
public static double averageLong(Collection<Long> values);
public static double averageFloat(Collection<Float> values);
public static double averageDouble(Collection<Double> values);
public static double averageByte(Byte... values);
public static double averageShort(Short... values);
public static double averageInteger(Integer... values);
public static double averageLong(Long... values);
public static double averageFloat(Float... values);
public static double averageDouble(Double... values);
public static double averageDouble(Double... values);
public static double varianceShort(Collection<Short> values);
public static double varianceInteger(Collection<Integer> values);
public static double varianceLong(Collection<Long> values);
public static double varianceFloat(Collection<Float> values);
public static double varianceDouble(Collection<Double> values);
public static double varianceByte(Byte... values);
public static double varianceShort(Short... values);
public static double varianceInteger(Integer... values);
public static double varianceLong(Long... values);
public static double varianceFloat(Float... values);
public static double varianceDouble(Double... values);
public static double standardDeviationByte(Collection<Byte> values);
public static double standardDeviationShort(Collection<Short> values);
public static double standardDeviationInteger(Collection<Integer> values);
public static double standardDeviationLong(Collection<Long> values);
public static double standardDeviationFloat(Collection<Float> values);
public static double standardDeviationDouble(Collection<Double> values);
public static double standardDeviationByte(Byte... values);
public static double standardDeviationShort(Short... values);
public static double standardDeviationInteger(Integer... values);
public static double standardDeviationLong(Long... values);
public static double standardDeviationFloat(Float... values);
public static double standardDeviationDouble(Double... values);