/
MathUtils.java
75 lines (65 loc) · 2.15 KB
/
MathUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package de.pxav.kelp.core.common;
import java.util.concurrent.ThreadLocalRandom;
/**
* A small collection of some mathematical utils for Kelp.
* This class is functional, so all methods can be accessed statically.
*
* @author pxav
*/
public class MathUtils {
private static final int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
private static final String[] romanLiterals = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
/**
* Checks if the given number x is even, which is
* equal to {@code x % 2 == 0}
*
* @param number The number to check.
* @return {@code true} if the number is even.
*/
public static boolean isEven(int number) {
return number % 2 == 0;
}
/**
* Checks if the given number x is odd, which is
* equal to {@code x % 2 == 1}
*
* @param number The number to check.
* @return {@code true} if the number is odd.
*/
public static boolean isOdd(int number) {
return number % 2 != 0;
}
/**
* Randomly calculates whether a certain chance has been
* fulfilled. If your {@code chance} is set to {@code 0.5},
* there will be a 50% chance that this method will return
* {@code true}. If it is {@code 0.05}, then there will be a
* {@code 5} per cent chance that this method will return
* {@code true}.
*
* @param chance The chance to use for random calculation.
* This number may range from 0.0 (always false, 0% change)
* and 1.0 (always true, 100% chance), where 0.01 is equal to
* 1%.
* @return Either true or false depending on your luck and the provided chance.
*/
public static boolean perCentChance(double chance) {
if (chance >= 1.0d) {
return true;
}
if (chance <= 0.0d) {
return false;
}
return Math.random() < chance;
}
public static String getRomanNumber(int number) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
s.append(romanLiterals[i]);
}
}
return s.toString();
}
}