/
MathUtils.java
56 lines (50 loc) · 1.47 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
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 {
/**
* 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 50},
* there will be a 50% chance that this method will return
* {@code true}. If it is {@code 5}, 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.
* @return Either true or false depending on your luck and the provided chance.
*/
public static boolean perCentChance(int chance) {
if (chance == 100) {
return true;
}
if (chance == 0) {
return false;
}
return ThreadLocalRandom.current().nextInt(0, 101) < chance;
}
}