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/ListUtils.java | |
| parent | 12637af8d6b7b9b2d96deb89e5a09e05178a8e65 (diff) | |
Removed old data types
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java | 76 |
1 files changed, 72 insertions, 4 deletions
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 |
