summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java
blob: 24c2014b4e9d742638277787549c42b8c764f971 (plain)
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
package bjc.utils.funcutils;

import java.util.function.Function;

/**
 * Utility functions for dealing with numbers
 * 
 * @author ben
 *
 */
public class NumberUtils {
	/**
	 * Compute the falling factorial of a number
	 * 
	 * @param value
	 *            The number to compute
	 * @param power
	 *            The power to do the falling factorial for
	 * @return The falling factorial of the number to the power
	 */
	public static int fallingFactorial(int value, int power) {
		if (power == 0) {
			return 1;
		} else if (power == 1) {
			return value;
		} else {
			int result = 1;

			for (int currentSub = 0; currentSub < power + 1; currentSub++) {
				result *= value - currentSub;
			}

			return result;
		}
	}

	/**
	 * Evaluates a linear probability distribution
	 * 
	 * @param winning
	 *            The number of winning possibilities
	 * @param total
	 *            The number of total possibilities
	 * @param rng
	 *            The function to use to generate a random possibility
	 * @return Whether or not a random possibility was a winning one
	 */
	public static boolean isProbable(int winning, int total,
			Function<Integer, Integer> rng) {
		return rng.apply(total) < winning;
	}
}