diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-13 16:54:12 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-13 16:54:12 -0400 |
| commit | ba07771f8333f1b098ab8a9ec9fec886b72b9cc0 (patch) | |
| tree | 7d1326235d021cb4767065cddd25bbe9fbdf5ce1 /BJC-Utils2/src/main/java/bjc/utils/funcutils | |
| parent | 12637af8d6b7b9b2d96deb89e5a09e05178a8e65 (diff) | |
Removed old data types
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils')
3 files changed, 127 insertions, 6 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java index f490727..3374aa2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -28,7 +28,7 @@ public class EnumUtils { Random rnd) { E[] enumValues = enumClass.getEnumConstants(); - return new FunctionalList<>(enumValues).randItem(rnd); + return new FunctionalList<>(enumValues).randItem(rnd::nextInt); } /** @@ -54,7 +54,7 @@ public class EnumUtils { int randomValueCount = enumValues.length - nValues; for (int i = 0; i <= randomValueCount; i++) { - E rDir = valueList.randItem(rnd); + E rDir = valueList.randItem(rnd::nextInt); valueList.removeMatching(rDir); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 871e1cf..132e4c5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,5 +1,6 @@ package bjc.utils.funcutils; +import java.util.ArrayList; import java.util.Deque; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -20,8 +21,8 @@ import bjc.utils.funcdata.IFunctionalList; public class ListUtils { private static final int MAX_NTRIESPART = 50; - private static final class TokenDeaffixer - implements BiFunction<String, String, IFunctionalList<String>> { + private static final class TokenDeaffixer implements + BiFunction<String, String, IFunctionalList<String>> { private String token; public TokenDeaffixer(String tok) { @@ -53,8 +54,8 @@ public class ListUtils { } } - private static final class TokenSplitter - implements BiFunction<String, String, IFunctionalList<String>> { + private static final class TokenSplitter implements + BiFunction<String, String, IFunctionalList<String>> { private String tokenToSplit; public TokenSplitter(String tok) { @@ -351,4 +352,71 @@ public class ListUtils { }); } } + + /** + * Select a number of random items from the list, with replacement + * + * @param <E> + * The type of items to select + * @param list + * The list to select from + * @param numberOfItems + * The number of items to selet + * @param rng + * A function that creates a random number from 0 to the + * desired number + * @return A new list containing the desired number of items randomly + * selected from the specified list + */ + public static <E> IFunctionalList<E> drawWithReplacement( + IFunctionalList<E> list, int numberOfItems, + Function<Integer, Integer> rng) { + IFunctionalList<E> selectedItems = new FunctionalList<>( + new ArrayList<>(numberOfItems)); + + for (int i = 0; i < numberOfItems; i++) { + selectedItems.add(list.randItem(rng)); + } + + return selectedItems; + } + + /** + * Select a number of random items from the list without replacement + * + * @param <E> + * The type of items to select + * @param list + * The list to select from + * @param numberOfItems + * The number of items to selet + * @param rng + * A function that creates a random number from 0 to the + * desired number + * @return A new list containing the desired number of items randomly + * selected from the specified list without replacement + */ + + public static <E> IFunctionalList<E> drawWithoutReplacement( + IFunctionalList<E> list, int numberOfItems, + Function<Integer, Integer> rng) { + IFunctionalList<E> selectedItems = new FunctionalList<>( + new ArrayList<>(numberOfItems)); + + int totalItems = list.getSize(); + + list.forEachIndexed((index, element) -> { + int winningChance = numberOfItems - selectedItems.getSize(); + // n - m + int totalChance = totalItems - (index - 1); + // N - t + + // Probability of selecting the t+1'th element + if (NumberUtils.isProbable(winningChance, totalChance, rng)) { + selectedItems.add(element); + } + }); + + return selectedItems; + } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java new file mode 100644 index 0000000..1f8f61d --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java @@ -0,0 +1,53 @@ +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 topExp + * The number of winning possibilities + * @param bottomExp + * 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 topExp, int bottomExp, + Function<Integer, Integer> rng) { + return rng.apply(bottomExp) < topExp; + } +} |
